Android 透明状态栏
轉(zhuǎn)載:https://blog.csdn.net/fan7983377/article/details/51604657
最近公司產(chǎn)品提出透明狀態(tài)欄的要求,將一張背景填充滿屏幕,自己記錄一下:
Android 透明狀態(tài)欄:有兩種,背景是圖片還是純色,下面分開(kāi)講:
1.當(dāng)背景為圖片時(shí),布局可以這么寫(xiě):
方法1,在代碼onCreate()方法里書(shū)寫(xiě)下面代碼:
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| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);window.setStatusBarColor(Color.TRANSPARENT);window.setNavigationBarColor(Color.TRANSPARENT); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {Window window = getWindow();window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); }然后在xml跟布局,設(shè)置屬性:android:fitsSystemWindows="true",代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_scenery"android:fitsSystemWindows="true"> </LinearLayout>效果圖:
方法二:主要的操作都在style.xml 和 AndroidManifest.xml 中,Activity里面沒(méi)有任何涉及到需要設(shè)置的代碼,所以可以忽略不看。
ps:
首先需要到AndroidManifest中為指定的Activity設(shè)置Theme。但是需要注意的是,我們不能直接在values/style.xml直接去自定義 Translucet System Bar 的Theme,因?yàn)楦奶匦詢(xún)H兼容 Android 4.4 開(kāi)始的平臺(tái),所以直接在values/style.xml聲明引入,工程會(huì)報(bào)錯(cuò)。有些開(kāi)發(fā)者朋友會(huì)在代碼中去判斷SDK的版本,然后再用代碼設(shè)置Theme。雖然同樣可以實(shí)現(xiàn)效果,但個(gè)人并不推崇這種做法。我所采取的方法則是建立多個(gè)SDK版本的values文件夾,系統(tǒng)會(huì)根據(jù)SDK的版本選擇合適的Theme進(jìn)行設(shè)置。大家可以看到上面我的工程里面有values、values-v19、values-v21。
該方法需要做下面三步設(shè)置:
step1:在values、values-v19、values-v21的style.xml都設(shè)置一個(gè) Translucent System Bar 風(fēng)格的Theme
values/style.xml
<style name="ImageTranslucentTheme" parent="AppTheme"><!--在Android 4.4之前的版本上運(yùn)行,直接跟隨系統(tǒng)主題--> </style>values-v19/style.xml
<style name="ImageTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"><item name="android:windowTranslucentStatus">true</item><item name="android:windowTranslucentNavigation">true</item> </style>values-v21/style.xml
<style name="ImageTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"><item name="android:windowTranslucentStatus">false</item><item name="android:windowTranslucentNavigation">true</item><!--Android 5.x開(kāi)始需要把顏色設(shè)置透明,否則導(dǎo)航欄會(huì)呈現(xiàn)系統(tǒng)默認(rèn)的淺灰色--><item name="android:statusBarColor">@android:color/transparent</item> </style>上面需要注意的地方是,無(wú)論你在哪個(gè)SDK版本的values目錄下,設(shè)置了主題,都應(yīng)該在最基本的values下設(shè)置一個(gè)同名的主題。這樣才能確保你的app能夠正常運(yùn)行在 Android 4.4 以下的設(shè)備。否則,肯定會(huì)報(bào)找不到Theme的錯(cuò)誤。
step2:在AndroidManifest.xml中對(duì)指定Activity的theme進(jìn)行設(shè)置
<activity android:name=".activity.ImageActivity"android:theme="@style/ImageTranslucentTheme"/>step3:在Activity的布局文件中設(shè)置背景圖片,同時(shí),需要把a(bǔ)ndroid:fitsSystemWindows設(shè)置為true
xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_building"android:fitsSystemWindows="true"> </LinearLayout>效果圖:
上面2中方法,如果僅僅需要ImageView控件覆蓋狀態(tài)欄,那么將android:fitsSystemWindows設(shè)置為false即可。
xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_building"android:fitsSystemWindows="false"><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:scaleType="fitXY"android:src="@drawable/hehua"/></LinearLayout>效果圖:
1.當(dāng)背景為純色時(shí),
由于它的Tab欄是純色的,所以只要把系統(tǒng)通知欄的顏色設(shè)置和Tab欄的顏色一致即可,實(shí)現(xiàn)上相比方法一要簡(jiǎn)單很多。同樣要到不同SDK版本的values下,創(chuàng)建一個(gè)同名的theme,在values-v21下,需要設(shè)置系統(tǒng)導(dǎo)航欄的顏色:
values/style.xml
<style name="ColorTranslucentTheme" parent="AppTheme"> </style>values-v19/style.xml
<style name="ColorTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"><item name="android:windowTranslucentStatus">true</item><item name="android:windowTranslucentNavigation">true</item> </style>values-v21/style.xml
<style name="ColorTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"><item name="android:windowTranslucentStatus">false</item><item name="android:windowTranslucentNavigation">true</item><item name="android:statusBarColor">@android:color/transparent</item> </style>在AndroidManifest.xml中對(duì)指定Activity的theme進(jìn)行設(shè)置
<!--圖片--> <activity android:name=".activity.ColorActivity"android:theme="@style/ColorTranslucentTheme"/>xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/bar_color"android:fitsSystemWindows="true"><!--標(biāo)題布局--><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="55dp"android:background="@color/bar_color"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="這是title欄"android:textColor="@android:color/white"android:textSize="20sp" /></RelativeLayout><!--內(nèi)容布局--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="@android:color/white"><!--自己根據(jù)實(shí)際需求填寫(xiě)--></LinearLayout> </LinearLayout>效果圖:
好了,以上就是沉浸式狀態(tài)欄實(shí)現(xiàn)的全過(guò)程,但是還有一點(diǎn)值得注意的就是,如果我們activity比較多,每一個(gè)頁(yè)面都添加android:fitsSystemWindows="true"?比較麻煩,我們需要改動(dòng)一下:
寫(xiě)一個(gè)基類(lèi)BaseColorActivity.class,代碼如下:
public abstract class BaseColorActivity extends AppCompatActivity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//這一行注意!看本文最后的說(shuō)明!!!!setContentView(getLayoutId());ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);View parentView = contentFrameLayout.getChildAt(0);if(parentView != null && Build.VERSION.SDK_INT >= 14){parentView.setFitsSystemWindows(true);}}protected abstract int getLayoutId(); }然后需要沉浸狀態(tài)欄的activity繼承該基類(lèi):
public class ColorActivity extends BaseColorActivity {public static void startActivity(Context context){Intent intent = new Intent(context, ColorActivity.class);context.startActivity(intent);}@Overrideprotected int getLayoutId() {return R.layout.activity_color;} }然后需要沉浸狀態(tài)欄的activity的布局文件中就可以把a(bǔ)ndroid:fitsSystemWindows="true"這行代碼給省略了!,其他設(shè)置還是按照一開(kāi)始操作。
?
寫(xiě)個(gè)工具類(lèi)StatusBarCompat.class:
public class StatusBarCompat {private static final int INVALID_VAL = -1;private static final int COLOR_DEFAULT = Color.parseColor("#20000000");@TargetApi(Build.VERSION_CODES.LOLLIPOP)public static void compat(Activity activity, int statusColor){//當(dāng)前手機(jī)版本為5.0及以上 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){if (statusColor != INVALID_VAL){activity.getWindow().setStatusBarColor(statusColor);}return;}//當(dāng)前手機(jī)版本為4.4if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){int color = COLOR_DEFAULT;ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);if (statusColor != INVALID_VAL){color = statusColor;}View statusBarView = new View(activity);ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight(activity));statusBarView.setBackgroundColor(color);contentView.addView(statusBarView, lp);}}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;}}使用方法:
在當(dāng)前activity的onCreate中,調(diào)用方法StatusBarCompat.compat就可以了:
//第二個(gè)參數(shù)是想要設(shè)置的顏色
StatusBarCompat.compat(this, Color.RED);
如果嫌每個(gè)activity都要寫(xiě)有點(diǎn)麻煩,那就寫(xiě)個(gè)基類(lèi)來(lái)完成這一步:
public class BaseActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {supportRequestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);StatusBarCompat.compat(this, Color.RED);} }關(guān)于上面代碼中提示注意的那個(gè)地方的說(shuō)明:
隱藏系統(tǒng)title注意的兩點(diǎn):
繼承AppCompatActivity時(shí)使用:?
supportRequestWindowFeature(Window.FEATURENOTITLE)
繼承activity時(shí)使用:?
requestWindowFeature(Window.FEATURENOTITLE)?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Android 透明状态栏的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 1218
- 下一篇: android 字体加下划线,如何在An