Android 修改狀態欄的方法
/**
* 活動基類,其他子活動繼承就可以了
*/
public class BaseActivity extends AppCompatActivity {
/**
* 狀態欄的顏色
*/
public static final int THIS_STATES_BAR_COLOR = R.color.vx_top_black;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
// 設定狀態欄背景
setThisStatusBarColor(THIS_STATES_BAR_COLOR);
}
/**
* 修改狀態欄的背景
* @param resId 顏色id
*/
public void setThisStatusBarColor(final int resId) {
getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//利用反射機制修改狀態欄背景
int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
View statusBarView = getWindow().findViewById(identifier);
// 這里傳入想設定的顏色
statusBarView.setBackgroundResource(resId);
getWindow().getDecorView().removeOnLayoutChangeListener(this);
}
});
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/226253.html
標籤:其他
上一篇:創建一個懸浮窗 Activity(或者無界面Activity)
下一篇:flutter視頻播放器
