Android編程中沉浸式狀態(tài)欄的三種實(shí)現(xiàn)方式詳解
本文實(shí)例講述了Android編程中沉浸式狀態(tài)欄的三種實(shí)現(xiàn)方式。分享給大家供大家參考,具體如下:
沉浸式狀態(tài)欄
Google從android kitkat(Android 4.4)開始,給我們開發(fā)者提供了一套能透明的系統(tǒng)ui樣式給狀態(tài)欄和導(dǎo)航欄,這樣的話就不用向以前那樣每天面對(duì)著黑乎乎的上下兩條黑欄了,還可以調(diào)成跟Activity一樣的樣式,形成一個(gè)完整的主題,和IOS7.0以上系統(tǒng)一樣了。
首先看下效果
首先看下第一種方式
系統(tǒng)的方式沉浸式狀態(tài)欄實(shí)現(xiàn)
步奏一
//當(dāng)系統(tǒng)版本為4.4或者4.4以上時(shí)可以使用沉浸式狀態(tài)欄 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); }
步奏二
布局加入:
android:fitsSystemWindows="true" android:clipToPadding="true"
我們看下activity和布局文件
FirstActivity.java:
/** * 沉浸式狀態(tài)欄 */ private void initState() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } }
activity_first.xml:
<?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"> <TextView android:fitsSystemWindows="true" android:clipToPadding="true" android:layout_width="match_parent" android:layout_height="140dp" android:textSize="24dp" android:background="@color/mask_tags_1" android:text="你好,沉浸式狀態(tài)欄"/> </LinearLayout>
接著看下第二種方式
實(shí)現(xiàn)思路,添加隱藏布局,然后我們動(dòng)態(tài)的計(jì)算狀態(tài)欄的高度,然后把這個(gè)高度設(shè)置成這個(gè)隱藏的布局的高度,便可以實(shí)現(xiàn)
在這里我們通過反射來獲取狀態(tài)欄的高度
/** * 通過反射的方式獲取狀態(tài)欄高度 * * @return */ private int getStatusBarHeight() { try { Class<?> c = Class.forName("com.android.internal.R$dimen"); Object obj = c.newInstance(); Field field = c.getField("status_bar_height"); int x = Integer.parseInt(field.get(obj).toString()); return getResources().getDimensionPixelSize(x); } catch (Exception e) { e.printStackTrace(); } return 0; }
來看下SecondActivity和布局文件吧
SecondActivity.java
package com.example.translucentbarstest; import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.WindowManager; import android.widget.LinearLayout; import java.lang.reflect.Field; /** * Created by 若蘭 on 2016/1/22. * 一個(gè)懂得了編程樂趣的小白,希望自己 * 能夠在這個(gè)道路上走的很遠(yuǎn),也希望自己學(xué)習(xí)到的 * 知識(shí)可以幫助更多的人,分享就是學(xué)習(xí)的一種樂趣 * QQ:1069584784 */ public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); initState(); } /** * 動(dòng)態(tài)的設(shè)置狀態(tài)欄 實(shí)現(xiàn)沉浸式狀態(tài)欄 * */ private void initState() { //當(dāng)系統(tǒng)版本為4.4或者4.4以上時(shí)可以使用沉浸式狀態(tài)欄 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); // LinearLayout linear_bar = (LinearLayout) findViewById(R.id.ll_bar); linear_bar.setVisibility(View.VISIBLE); //獲取到狀態(tài)欄的高度 int statusHeight = getStatusBarHeight(); //動(dòng)態(tài)的設(shè)置隱藏布局的高度 LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) linear_bar.getLayoutParams(); params.height = statusHeight; linear_bar.setLayoutParams(params); } } /** * 通過反射的方式獲取狀態(tài)欄高度 * * @return */ private int getStatusBarHeight() { try { Class<?> c = Class.forName("com.android.internal.R$dimen"); Object obj = c.newInstance(); Field field = c.getField("status_bar_height"); int x = Integer.parseInt(field.get(obj).toString()); return getResources().getDimensionPixelSize(x); } catch (Exception e) { e.printStackTrace(); } return 0; } }
activity_second.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.example.translucentbarstest.TwoActivity"> <!--這個(gè)是隱藏的布局,然后通過動(dòng)態(tài)的設(shè)置高度達(dá)到效果--> <LinearLayout android:id="@+id/ll_bar" android:layout_width="fill_parent" android:layout_height="1dp" android:orientation="vertical" android:background="#e7abff" android:visibility="gone"> </LinearLayout> <TextView android:fitsSystemWindows="true" android:clipToPadding="true" android:layout_width="match_parent" android:layout_height="140dp" android:background="@color/mask_tags_3" android:text="你好,沉浸式狀態(tài)欄"/> </LinearLayout>
接下來看下第三種
這個(gè)是用的github上的第三方庫
1.庫地址:https://github.com/jgilfelt/SystemBarTint
2.添加依賴庫:
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
步奏一
android:fitsSystemWindows="true" android:clipToPadding="true
步奏二
SystemBarTintManager tintManager = new SystemBarTintManager(this); // 激活狀態(tài)欄 tintManager.setStatusBarTintEnabled(true); // enable navigation bar tint 激活導(dǎo)航欄 tintManager.setNavigationBarTintEnabled(true); //設(shè)置系統(tǒng)欄設(shè)置顏色 //tintManager.setTintColor(R.color.red); //給狀態(tài)欄設(shè)置顏色 tintManager.setStatusBarTintResource(R.color.mask_tags_1); //Apply the specified drawable or color resource to the system navigation bar. //給導(dǎo)航欄設(shè)置資源 tintManager.setNavigationBarTintResource(R.color.mask_tags_1);
來看下代碼吧
ThreeActivity.java
package com.example.translucentbarstest; import android.os.Build; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.WindowManager; import com.readystatesoftware.systembartint.SystemBarTintManager; /** * Created by 若蘭 on 2016/1/22. * 一個(gè)懂得了編程樂趣的小白,希望自己 * 能夠在這個(gè)道路上走的很遠(yuǎn),也希望自己學(xué)習(xí)到的 * 知識(shí)可以幫助更多的人,分享就是學(xué)習(xí)的一種樂趣 * QQ:1069584784 */ public class ThreeActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_three); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); SystemBarTintManager tintManager = new SystemBarTintManager(this); // 激活狀態(tài)欄 tintManager.setStatusBarTintEnabled(true); // enable navigation bar tint 激活導(dǎo)航欄 tintManager.setNavigationBarTintEnabled(true); //設(shè)置系統(tǒng)欄設(shè)置顏色 //tintManager.setTintColor(R.color.red); //給狀態(tài)欄設(shè)置顏色 tintManager.setStatusBarTintResource(R.color.mask_tags_1); //Apply the specified drawable or color resource to the system navigation bar. //給導(dǎo)航欄設(shè)置資源 tintManager.setNavigationBarTintResource(R.color.mask_tags_1); } } }
activity_three.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:background="#ffff" android:orientation="vertical" tools:context="com.example.translucentbarstest.ThirdActivity"> <TextView android:layout_width="match_parent" android:layout_height="140dp" android:background="@color/mask_tags_5" android:clipToPadding="true" android:fitsSystemWindows="true" android:text="你好,沉浸式狀態(tài)欄" android:textSize="24dp"/> </LinearLayout>
好了,原來自己以為沉浸式狀態(tài)欄聽著好厲害(有可能自己原先不知道),但是真正自己去做了,去了解了,也沒有那么難、那么神秘了,我想這也是自己成長(zhǎng)了一些。
繼續(xù)努力。這個(gè)是上傳的github上的demo: https://github.com/wuyinlei/-
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android操作XML數(shù)據(jù)技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android開發(fā)入門與進(jìn)階教程》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android Studio 全屏沉浸式透明狀態(tài)欄效果的實(shí)現(xiàn)
- Android實(shí)現(xiàn)沉浸式狀態(tài)欄功能
- Android沉浸式狀態(tài)欄 + actionBar漸變 + scrollView頂部伸縮效果
- Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼
- Android沉浸式狀態(tài)欄設(shè)計(jì)的實(shí)例代碼
- 解決Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題
- Android 沉浸式狀態(tài)欄與隱藏導(dǎo)航欄實(shí)例詳解
- Android 詳解沉浸式狀態(tài)欄的實(shí)現(xiàn)流程
相關(guān)文章
Diycode開源項(xiàng)目實(shí)例搭建上拉加載和下拉刷新的Fragment
這篇文章主要介紹了Diycode開源項(xiàng)目實(shí)例搭建上拉加載和下拉刷新的Fragment以及相關(guān)的代碼分享。2017-11-11Android View添加 Listener 實(shí)例代碼
在開發(fā)中為控件添加Listener是非常常見的工作,最簡(jiǎn)單的添加Listener方式可以這樣2013-05-05Android實(shí)現(xiàn)歌詞滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)歌詞滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android自定義View控件實(shí)現(xiàn)刷新效果
這篇文章主要介紹了Android自定義View控件實(shí)現(xiàn)刷新效果的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11Android導(dǎo)航欄功能項(xiàng)的顯示與屏蔽介紹
大家好,本篇文章主要講的是Android導(dǎo)航欄功能項(xiàng)的顯示與屏蔽介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云(四)
這篇文章主要為大家詳細(xì)介紹了Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Android AlertDialog實(shí)現(xiàn)分享對(duì)話框/退出對(duì)話框/下載對(duì)話框
這篇文章主要介紹了Android AlertDialog實(shí)現(xiàn)分享對(duì)話框/退出對(duì)話框/下載對(duì)話框的相關(guān)資料,需要的朋友可以參考下2016-04-04