自定義狀态欄背景(statusbar)
發表時(shí)間:2021-1-10
發布人(rén):融晨科技
浏覽次數:55
公司大(dà)設計師隻出(chū)iOS設計圖。新的(de)iOS系統的(de)狀态欄(status bar,就(jiù)是(shì)顯示時(shí)間、電量那個(gè))是(shì)透明的(de),Android 4.4 (Kitkat,api 19)也(yě)添加了(le/liǎo)自定義 status bar 和(hé / huò) navigation bar 的(de)一些api,可以(yǐ)實現同樣的(de)效果。
---
github上(shàng)有 SystemBarTint 可以(yǐ)使用,但是(shì)很多功能用不(bù)到(dào),所以(yǐ)自己實現了(le/liǎo)下。
---
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setStatusBarColor(android.R.color.transparent); } } /** * 使status bar 和(hé / huò) navigation bar 透明 */ private void setStatusBarColor(int colorId) { int flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; getWindow().addFlags(flags); int statusBarHeight = getStatusBarHeight(this); View view = new View(this); view.setBackgroundResource(colorId); ViewGroup parent = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);//parent是(shì)setContentView(content)中content的(de)父view parent.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight); parent.getChildAt(0).setPadding(0, statusBarHeight, 0, 0); } private int getStatusBarHeight(Context context) { int id = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); int dimen = 0; if (id > 0) dimen = getResources().getDimensionPixelSize(id); return dimen; } }
---
DecorView的(de)相關知識補充(Debug模式下一點點查看出(chū)來(lái)的(de)):
[img]http://img.blog.csdn.net/20150108140856496