Android,沉浸式状态栏,状态栏以及Toolbar颜色分开设置
2017-11-03 11:36
711 查看
这里的状态栏和Toolbar 颜色是分开的,按照这几个步骤就可以实现 ,请看代码 :
首先 :
/** * 设置状态栏颜色 */ public class StatusBarUtils { private static final int INVALID_VAL = -1; @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static View compat(Activity activity, int statusColor) { int color = ContextCompat.getColor(activity, R.color.colorPrimaryDark); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (statusColor != INVALID_VAL) { color = statusColor; } activity.getWindow().setStatusBarColor(color); return null; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); if (statusColor != INVALID_VAL) { color = statusColor; } View statusBarView = contentView.getChildAt(0); if (statusBarView != null && statusBarView.getMeasuredHeight() == getStatusBarHeight(activity)) { statusBarView.setBackgroundColor(color); return statusBarView; } statusBarView = new View(activity); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); statusBarView.setBackgroundColor(color); contentView.addView(statusBarView, lp); return statusBarView; } return null; } public static void compat(Activity activity) { compat(activity, INVALID_VAL); } 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; } }
基本页面布局 :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:fitsSystemWindows="true" //最好加上这句话 -->所有组件都要在屏幕范围之内 tools:context="ce.mobsmstest.TestStatusActivity"> <include layout="@layout/toolbar"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#ff3" android:text="测试沉浸式状态栏" android:textColor="@android:color/black" android:textSize="20sp"/> </LinearLayout>
toolbar布局 :
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize"> <!--<ImageView android:id="@+id/iv_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@android:color/white" android:src="@drawable/ic_back"/>--> <TextView android:id="@+id/toolbar_title" style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="21sp" tools:text="中间"/> <TextView android:id="@+id/toolbar_menu_title" style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:paddingRight="15dp" android:textSize="16sp" tools:text="右面"/> </android.support.v7.widget.Toolbar>
MainActivity 代码设置 :
public class TestStatusActivity extends AppCompatActivity { protected int statusBarColor = 0; protected View statusBarView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_status); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle(""); // ----->设置 Toolbar颜色为绿色,所有设置要在 setSupportActionBar()之前 toolbar.setBackgroundResource(R.color.lv_se); setSupportActionBar(toolbar); if (statusBarColor == 0) { //----> 设置状态栏颜色为黑色 statusBarView = StatusBarUtils.compat(this, ContextCompat.getColor(this, R.color.black)); } else if (statusBarColor != -1) { statusBarView = StatusBarUtils.compat(this, statusBarColor); } initStatus(); } protected void initStatus() { // 判断当前系统是不是大于19, 4.4版本以上 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //透明状态栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } }
整体效果 :
状态栏 : ---->黑色
Toolbar : ------> 绿色
额外补充知识点 : 动态设置高度
1. 在代码里面设置不要状态栏
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无ActionBar
2. 使用类似布局 :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ggxiaozhi.store.the_basket.mvp.view.activity.HomeActivity">
<LinearLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black_alpha_5"
android:orientation="vertical">
<LinearLayout
android:id="@+id/status_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="46dp"
app:tabIndicatorColor="@color/tab_selected_line_def"
app:tabSelectedTextColor="@color/tab_text_selected_def"
app:tabTextAppearance="@style/CustomTabLayoutTextAppearance"
app:tabTextColor="@color/tab_text_normal_def"/>
<!--tabIndicatorColor下方滚动的下划线颜色 -->
<!--tabTextAppearance设置文字的外貌 -->
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/main_viewPage"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
3. 通过反射获取状态栏的高度
/**
* 获取状态栏的高度
* @return状态栏的高度
*/
protected int getStatusBarHeight(){
try {
//通过反射获取到类
Class<?> aClass=Class.forName("com.android.internal.R$dimen");
//创建对象
Object o=aClass.newInstance();
//拿取属性
Field status_bar_height = aClass.getField("status_bar_height");
//获取值
Object o1=status_bar_height.get(o);
int height=Integer.parseInt(o1.toString());
return getResources().getDimensionPixelOffset(height);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
4. 把状态栏的高度设置给布局里面的 linearlayout
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { final int statusHeight = getStatusBarHeight(); status_bar.post(new Runnable() { @Override public void run() { android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) status_bar.getLayoutParams(); params.height = statusHeight; status_bar.setLayoutParams(params); } }); }
再介绍一种实现方式 :
Qmui --> 腾讯团队开发的UI框架,具体使用,自行百度,里面有沉浸式状态栏,具体实现,请看Demo
相关文章推荐
- Android ToolBar 解析与应用(三)状态栏,toolbar,窗口背景,底部导航条颜色设置,
- android沉浸式 & 状态栏颜色设置
- Android沉浸式(设置状态栏颜色)
- Android设置沉浸式状态栏时改变状态栏的颜色(只对MIUI V6可用)
- Android开发 -- android设置系统状态栏颜色(沉浸式状态栏)
- Android状态栏颜色设置(沉浸式状态栏)
- Android沉浸式标题栏状态栏字体颜色修改(小米和魅族以及Android6.0)
- android设置状态栏颜色(沉浸式状态栏)
- 设置android状态栏颜色和toolbar颜色一致
- 如何设置android 5.0主题,状态栏,toolbar颜色设定
- Android 设置状态栏颜色&&沉浸式状态栏
- Android 沉浸式状态栏以及 透明状态栏 和修改状态栏颜色
- Android 沉浸式状态栏 一设置状态栏颜色
- Android 沉浸式状态栏-字体颜色与背景颜色修改实现与兼容
- Android给Activity状态栏设置自定义颜色
- Android StatusBar 状态栏颜色设置
- android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)
- android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)
- android 隐藏状态栏和修改状态栏的背景颜色(沉浸式状态栏)
- 设置Android系统的状态栏颜色(从Android4.4开始)