状态栏的背景两种设置方式
1.设置为全透明(andorid6.0以上支持),参考郭霖大神的博客即可:http://blog.csdn.net/guolin_blog/article/details/51763825
2.设置状态栏背景颜色,主要参考鸿洋大神的:http://blog.csdn.net/lmj623565791/article/details/48649563
看第二篇的时候略感难解,且不知为何源码我无法运行,略加改动,且转换为Kotlin
最终效果 从左至右 版本依次是 Android4.4以下 Android4.4-5.0 Android5.0以上
实现原理:Android4.4以下 无法实现 Android4.4-5.0设置半透明,且设置颜色 Android5.0以上直接设置背景色
values/styles.xml
<resources> <style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primary_dark</item> <item name="colorAccent">#FF4081</item> </style> <!-- Base application theme. --> <style name="AppTheme" parent="@style/BaseAppTheme"> </style> </resources>values-v19/styles.xml
<resources> <style name="AppTheme" parent="@style/BaseAppTheme"> <item name="android:windowTranslucentStatus">true</item> </style> </resources>
values-v21/styles.xml
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="@style/BaseAppTheme"> </style> </resources>
color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="primary">#FF03A9F4</color> <color name="primary_dark">#FF0288D1</color> <color name="status_bar_color">@color/primary_dark</color> </resources>
acticity-main.xml :注意里边fitsSystemWindows属性的位置是在最外层设置的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:fitsSystemWindows="true" > <TextView android:id="@+id/id_toolbar" android:layout_width="match_parent" android:layout_height="80dp" android:background="?attr/colorPrimary" android:visibility="visible" android:text="标题" android:gravity="center" android:textSize="24sp" /> <TextView android:id="@+id/id_tv_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="HelloWorld" android:textSize="30sp"/> </LinearLayout>
package com.example.minlewan.teststatusbar import android.app.Activity import android.view.ViewGroup import android.os.Build import android.annotation.TargetApi import android.content.Context import android.graphics.Color import android.graphics.Color.parseColor import android.view.View object StatusBarCompat { private val INVALID_VAL = -1 private val COLOR_DEFAULT = Color.parseColor("#20000000") @TargetApi(Build.VERSION_CODES.LOLLIPOP) fun compat(activity: Activity, statusColor: Int) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (statusColor != INVALID_VAL) { activity.window.statusBarColor = statusColor } return } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { var color = COLOR_DEFAULT val contentView = activity.findViewById<View>(android.R.id.content) as ViewGroup if (statusColor != INVALID_VAL) { color = statusColor } val statusBarView = View(activity) val lp = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)) statusBarView.setBackgroundColor(color) contentView.addView(statusBarView, lp) } } fun compat(activity: Activity) { compat(activity, INVALID_VAL) } fun getStatusBarHeight(context: Context): Int { var result = 0 val resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android") if (resourceId > 0) { result = context.getResources().getDimensionPixelSize(resourceId) } return result } }
MainActivity
class MainActivity : AppCompatActivity() { @RequiresApi(Build.VERSION_CODES.LOLLIPOP) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.colorAccent)) }对的,只要上边都配置好了, 只需添加一句话 StatusBarCompat.compat()即可设置颜色
无意要分,选择了最少的2分
csdn代码地址:http://download.csdn.net/download/minle_/10217894
github求顺手给个星星,不给也理解。毕竟简单
github地址:https://github.com/WanMinle/-statusbar-