美文网首页
如何自定义Android状态栏颜色

如何自定义Android状态栏颜色

作者: 一点墨汁 | 来源:发表于2017-06-28 15:31 被阅读66次
// 定义颜色值
private int[] statusColors = new int[]{R.color.color1_, R.color.color2_, 
R.color.color3_, R.color.color4_, R.color.color5_};
 // 设置Theme
 setCurrentTheme(ContextCompat.getColor(MainActivity.this, statusColors[currentColor]));
 private void setCurrentTheme(int statusColor) {
       if (Build.VERSION.SDK_INT >= 21) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            getWindow().setStatusBarColor(statusColor);
          
        } else if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | 
                                                localLayoutParams.flags);

            ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
            View statusBarView = contentView.getChildAt(0);
            if (statusBarView != null && statusBarView.getMeasuredHeight() == getStatusBarHeight(this)) {
                statusBarView.setBackgroundColor(statusColor);
                return;
            }
            statusBarView = new View(this);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight(this));
            statusBarView.setBackgroundColor(statusColor);
            contentView.addView(statusBarView, lp);
        }
}
       
// values-v19
 <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:fitsSystemWindows">true</item>
    </style>
//values-v21
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/black</item>
        <!--<item name="android:windowTranslucentStatus">true</item>-->
        <item name="android:fitsSystemWindows">true</item>
    </style>
   <activity
        android:name=".MainActivity"
       android:configChanges="orientation|screenSize"
       android:label="@string/app_name"
       android:launchMode="singleTask"
       android:screenOrientation="portrait"
       android:theme="@style/AppTheme.NoActionBar">

相关文章

网友评论

      本文标题:如何自定义Android状态栏颜色

      本文链接:https://www.haomeiwen.com/subject/dupucxtx.html