赞
踩
在很多情况下,我们都需要让项目的某个界面状态栏设置为透明或者直接隐藏起来,这样使我们的界面看起来整体美观大方一些。下面来给大家演示一下如何将Android状态栏设置为透明或者将状态栏隐藏起来。
我个人看博客是比较喜欢先看效果图,所以废话8多说,先上图:
铁子们看到这个效果图舒服了吗?舒服了就撸起袖子干吧!
上代码:
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<!--透明-->
<color name="transparent">#00000000</color>
<!--半透明-->
<color name="translucent">#22050505</color>
</resources>
public class StatusBar { private Activity activity; //初始化activity public StatusBar(Activity activity){ this.activity = activity; } //将状态栏设置为传入的color public void setColor(int color){ if (Build.VERSION.SDK_INT >= 21) { View view = activity.getWindow().getDecorView(); view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); activity.getWindow().setStatusBarColor(activity.getResources().getColor(color)); } } //隐藏状态栏 public void hide(){ if (Build.VERSION.SDK_INT >= 21) { activity.getWindow() .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); } } //设置状态栏字体颜色 public void setTextColor(boolean isDarkBackground){ View decor = activity.getWindow().getDecorView(); if (isDarkBackground) { //黑暗背景字体浅色 decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); } else { //高亮背景字体深色 decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } } }
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); StatusBar statusBar = new StatusBar(MainActivity.this); //设置颜色为半透明 statusBar.setColor(R.color.translucent); //设置颜色为透明 statusBar.setColor(R.color.transparent); //隐藏状态栏 statusBar.hide(); setContentView(R.layout.activity_main); } }
最后喜欢的可以关注一下我!我会一直更新Android的一些知识,谢谢大家(=v=)~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。