Android 4.4以后设置状态栏颜色
16lz
2021-01-23
前置条件:Android开发包4.4或以上
创建布局:布局文件
<RelativeLayout 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" tools:context="com.cn.statusbar.MainActivity" > <View android:id="@+id/statusBarBackground1" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" /></RelativeLayout>
第二步:创建Activity
package com.cn.statusbar;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Build;import android.os.Bundle;import android.util.TypedValue;import android.view.View;import android.view.Window;import android.view.WindowManager;@SuppressLint("InlinedApi")public class MActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white)); } public void setStatusBarColor(View statusBar,int color){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window w = getWindow(); w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //status bar height int actionBarHeight = getActionBarHeight(); int statusBarHeight = getStatusBarHeight(); //action bar height statusBar.getLayoutParams().height = actionBarHeight + statusBarHeight; statusBar.setBackgroundColor(color); } } public int getActionBarHeight() { int actionBarHeight = 0; TypedValue tv = new TypedValue(); if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics()); } return actionBarHeight; } public int getStatusBarHeight() { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; }}
OK了,看看代码就知道什么意思了
更多相关文章
- Android软键盘弹出不影响布局的方法
- 相对布局
- Android Relativelayout布局属性
- Android 各种基础控件布局
- Android布局之LinearLayout
- 线性布局LinearLayout和相对布局RelativeLayout 比较
- android界面布局
- ViewGroup常用布局操作