第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > Android自定义状态栏颜色

Android自定义状态栏颜色

时间:2019-10-28 13:16:24

相关推荐

Android自定义状态栏颜色

转载请注明出处:/crapulencezzz/article/details/52895475

本篇博客参考:

郭霖:Android状态栏微技巧,带你真正理解沉浸式模式:/sinyu890807/article/details/51763825鸿洋_:Android沉浸式状态栏攻略 让你的状态栏变色吧:/lmj623565791/article/details/48649563

在日常的开发中,为了保证App风格的统一,很多时候我们需要自定义状态栏的颜色。其实里面的坑还蛮多的,因为涉及到版本的兼容性。好了,废话不多说,开始今天的正题。

我们今天的效果是要做成QQ那样的效果,如图(MX2,Android4.4.4):

想要实现这种效果其实并不难,但是由于Android版本众多,各版本API并不能很好的兼容,造成实现过程可能会有一些bug,但是,不怕,有博主在,坑都踩的差不多了。

方式一:通过设置不同的styles,并在代码里判断API等级来动态设置状态栏颜色(兼容到Android 4.4,API19以上)

默认效果如下:

我们最终的效果是把它的颜色设置成QQ状态栏的颜色。根据我有限的经验,貌似Android 4.3 API18(包括)以下是不可以修改状态栏颜色的,而Android 5.0 API21以后是可以直接在代码或者styles里面设置状态栏颜色的。所以我们的实现就分两种情况,一种是Android4.4(API 19)~Android4.4(API 20),另一种是Android5.0(API 21)以上。

在 values/styles 里设置主题:(注意继承的为一个兼容主题,之前用的 android:Theme.Holo.Light.NoActionBar 不好用)

<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">...</style>

在 values-v19 里设置主题:

<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar"><item name="android:windowTranslucentStatus">true</item></style>

在 values-v21 里设置主题:(我们这里只需要修改statusBarColor为需要的颜色就可以了,这种是在文件中写死了,后面还会讲API21以后怎么在代码里动态设置状态栏的颜色)

<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar"><item name="android:colorPrimaryDark">@color/primary_black</item> //这个用来设置顶部状态栏和底部导航栏的颜色<item name="android:statusBarColor">@color/primary_black</item> //这个用来设置顶部状态栏颜色<item name="android:navigationBarColor">@color/green_5a</item>//这个用来设置底部导航栏颜色</style>

这时候运行效果如下:

我们发现布局竟然都顶到状态栏去了,这是由于 API 19 的主题里设置的状态栏为透明的,系统默认它将不占用空间,导致你自己写的布局会顶上去,所以还需要在代码里额外设置状态栏的颜色和布局的排版。先看一下之前错误的代码,注意这段代码必须写在 setContentView 以后(比如可以写在 BaseActivity 的 onStart() 中):

if(Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {ViewGroup contentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);if(contentView.getChildAt(0) != null) {contentView.getChildAt(0).setFitsSystemWindows(true); //fitSystemWindow:标识布局调整时是否考虑系统窗口(如状态栏)contentView.getChildAt(0).setBackgroundColor(getResources().getColor(R.color.primary_black));}}

运行效果如下:

我们这里是先拿到整个布局,然后拿到它的第一个子孩子,即你XML文件里写的根布局,然后设置它的属性。这里错就错在设置成了根布局的整体颜色,这就造成了你所有的布局都有了统一的背景。

修改后的代码:

@Overrideprotected void onStart() {super.onStart();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);View statusBarView = new View(this);ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this));statusBarView.setBackgroundColor(getResources().getColor(R.color.primary_black));contentView.getChildAt(0).setFitsSystemWindows(true);contentView.addView(statusBarView, lp);}}public static int getStatusBarHeight(Context context) {int result = 0;int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");if (resourceId > 0) {result = context.getResources().getDimensionPixelSize(resourceId);}return result;}

通过拿到状态栏的高度,只单独设置它的颜色,对根布局没有影响。最后的效果如下:

另外,为了能够像饿了么那样的效果,就是把状态栏设置成透明色,不过这个效果只有在5.0以后才可以,实现代码如下(再说一遍,必须在setContentView之后!):

if (Build.VERSION.SDK_INT >= 21) {View decorView = getWindow().getDecorView();int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;decorView.setSystemUiVisibility(option);getWindow().setStatusBarColor(Color.TRANSPARENT); //这里可以动态设置状态栏的颜色}//如果主题设置的是NoActionBar,就不需要下面的代码了ActionBar actionBar = getSupportActionBar();actionBar.hide();

效果如下:

方式二:先把状态栏设置成透明的,然后设置属性和背景

在代码里或者styles里配置,让状态栏变透明:

在代码里设置:getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);或者在styles里对应Activity的theme里添加:<item name="android:windowTranslucentStatus">true</item>

在代码或者根布局里配置:

在setContentView后设置:ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);ViewGroup childView = (ViewGroup) contentView.getChildAt(0);childView.setFitsSystemWindows(true);childView.setClipToPadding(true);childView.setBackgroundColor(getResources().getColor(R.color.primaryOrange_f6));或者在根布局里设置:android:background="@color/color_qq_blue"android:fitsSystemWindows="true"android:clipToPadding="true"

运行后效果如下:

这种还是把整个根布局设置成了固定的颜色,和上面的错误一样,在写的时候还要再套一层布局,类似下面的代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><!--在根布局下再嵌套一层布局--><LinearLayout android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/white"> //这里设置的是状态栏下面你自己应用界面的背景<TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@android:color/black"android:text="Hello World!"/></LinearLayout></LinearLayout>

综上:方式二适用于项目刚开始,或者还没有多少个页面的时候,因为用这种写法需要把每个xml布局都内嵌一层,不推荐使用。而方式一可以只拿到状态栏高度后单独设置状态栏的颜色,对根布局没有影响,我现在项目中用的也是方式一。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。