Android开发改变状态栏的颜色

现在,随着Android版本的更新,Android可设计的界面越来越美观,死板的统一不变的状态栏也逐渐被UI们放弃,状态栏的颜色也成为了许多人的问题;

这里,我讲一讲最近查资料和自己总结的关于修改状态栏颜色的问题;


Android版本4.4以后,提供了设置透明状态栏的方法;

Android版本5.0后,提供了设置状态栏颜色的方法;

包括QQ在内,状态栏与标题栏颜色一致,只有在Android4.4或以上的版本才实现;


从网上找了很多例子,但很多都达不到想要的结果,都是状态栏的颜色非常深,整个界面就不怎么美观了;

通过自己研究和看一些比较成熟的代码,找到了两种方式可以实现比较好的效果:

第一种,是我自己结合了一些例子写成的,该上代码了:

先在BaseActivity中写如下代码

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        }
        super.onCreate(savedInstanceState);
	}
}                                                                                                                       
    算了,这这样写吧,不知道怎么调成文本编辑模式了。。。。。
    然后在让自己的需要实现的自定义颜色的Activity继承BaseActivty;
    在该Activity的最外层布局中添加
	android:fitsSystemWindows="true" 
    设置最外层布局背景颜色为你想要状态栏的颜色,这样你的Activity的状态栏就会变成和你设置的背景颜色一个颜色了,当然,这个颜色最好是和你的标题栏的颜色一致
	这样才能达到颜色的统一;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorPrimary"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">
效果图如下:

当然 如果你想要我这样布局底层的背景色与最外层的背景色不一致,得单独为底层设置背景色;


第二种方法的BaseActivity的代码不变,同样用activity来继承;
在activity的布局文件中<include>插入title_layout.xml文件,title_layout.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayo ut xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:gravity="center"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/top_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
         />

        <TextView
            android:id="@+id/top_center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/top_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />
    </LinearLayout>
</LinearLayout>

	就是一个自定义标题栏,还是那个重要属性android:fitsSystemWindows="true"必须写;
	这样,其实已经出来了,但是你会发现你设置标题栏虽然没有顶到状态栏上去,但是显示在状态栏的实际高度是你设置的高度-状态栏的高度,这样就需要你们UI给你们一个整体设计的高度了,
如果UI给的是单独的话,那只有动态设置标题栏的高度了,算出状态栏的高度,然后相加得打最后的值;
	从第二种放法其实还可以引出另一种放法,就是算出状态栏的高度后,直接在根视图里加入一个与状态栏等高的空控件,背景设置为与标题栏一致;但是在每个activity的布局中都加入一个view,显然太浪费资源了;

	总算完了,下班了;
	




	





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值