Android修改状态栏背景色

来源:互联网 发布:ichart.js下载 编辑:程序博客网 时间:2024/05/18 03:18

这是小弟我第一次写博客,如有不足的地方,敬请原谅,有点紧张现在。

当我们在进行安卓开发的时候,我们经常会发现,当我们给一个页面设置标题栏的时候,往往其背景色又和手机自带的状态栏背景色不一样,但又发现很多NB的应用其颜色又都是设置成一样的,那么他们是怎么做的呢,请看下面的步骤:

1.首先在所布局的activity中添加以下两个属性:

android:clipToPadding="true"android:fitsSystemWindows="true"
这是为了防止我们的标题栏和状态栏重叠到一起

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:background="@color/theambacground"    android:clipToPadding="true"    android:fitsSystemWindows="true"    android:orientation="vertical">


2.我们为了方便此方法的调用和功能的实现,可以将相应的方法放在application里面

public  void initSystemBar(Activity activity,int color_id) {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {        setTranslucentStatus(activity, true);    }    SystemBarTintManager tintManager = new SystemBarTintManager(activity);    tintManager.setStatusBarTintEnabled(true);    // 使用颜色资源    tintManager.setStatusBarTintResource(color_id);}@TargetApi(19)private  void setTranslucentStatus(Activity activity, boolean on) {    Window win = activity.getWindow();    WindowManager.LayoutParams winParams = win.getAttributes();    final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;    if (on) {        winParams.flags |= bits;    } else {        winParams.flags &= ~bits;    }    win.setAttributes(winParams);}
3.方法的调用和参数的传递:

MyApplication.getApp().initSystemBar(this, R.color.theamcolor);
只需要将这行代码放入onCreate()方法里就可以了,这个activity的标题栏的颜色也就可以设置成自己想要的颜色了。




最后,欢迎大家关注和观看,如有不足的地方还请指教。


1 0
原创粉丝点击