【Android UI】状态栏和toolbar颜色一致

1、在style.xml中定义toolbar的颜色

 

[html] view plain copy

 print?

  1. <resources>  
  2.   
  3.     <!-- Base application theme. -->  
  4.     <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">  
  5.         <!-- Customize your theme here. -->  
  6.         <!-- toolbar(actionbar)颜色 -->  
  7.         <item name="colorPrimary">#4876FF</item>  
  8.         <item name="android:windowTranslucentStatus">true</item>  
  9.     </style>  
  10.   
  11.     <style name="AppTheme" parent="@style/AppBaseTheme"></style>  
  12.   
  13. </resources>  


2、布局文件添加属性

 

 

[plain] view plain copy

 print?

  1. android:fitsSystemWindows="true"  
  2. android:clipToPadding="true"  


3、在Activity的onCreate(Bundle savedInstanceState)方法中添加代码

 

 

[java] view plain copy

 print?

  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.   
  5.     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  
  6.         Window window = getWindow();  
  7.         window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS  
  8.                 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);  
  9.         window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN  
  10.                 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION  
  11.                 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);  
  12.         window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  
  13.         //获取样式中的属性值  
  14.         TypedValue typedValue = new TypedValue();  
  15.         this.getTheme().resolveAttribute(android.R.attr.colorPrimary, typedValue, true);  
  16.         int[] attribute = new int[] { android.R.attr.colorPrimary };  
  17.         TypedArray array = this.obtainStyledAttributes(typedValue.resourceId, attribute);  
  18.         int color = array.getColor(0, Color.TRANSPARENT);  
  19.         array.recycle();  
  20.   
  21.         window.setStatusBarColor(color);  
  22.     }  
  23.   
  24.     setContentView(R.layout.activity_main);  
  25.   
  26. }  

 

 

转载于:https://my.oschina.net/u/1177694/blog/1093975

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值