Android 修改状态栏的方法

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);
            }
        });
    }
}
相关文章
相关标签/搜索