Android編程中沉浸式狀態(tài)欄的三種實(shí)現(xiàn)方式詳解
本文實(shí)例講述了Android編程中沉浸式狀態(tài)欄的三種實(shí)現(xiàn)方式。分享給大家供大家參考,具體如下:
沉浸式狀態(tài)欄
Google從android kitkat(Android 4.4)開(kāi)始,給我們開(kāi)發(fā)者提供了一套能透明的系統(tǒng)ui樣式給狀態(tài)欄和導(dǎo)航欄,這樣的話(huà)就不用向以前那樣每天面對(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)
在這里我們通過(guò)反射來(lái)獲取狀態(tài)欄的高度
/**
* 通過(guò)反射的方式獲取狀態(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;
}
來(lái)看下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è)懂得了編程樂(lè)趣的小白,希望自己
* 能夠在這個(gè)道路上走的很遠(yuǎn),也希望自己學(xué)習(xí)到的
* 知識(shí)可以幫助更多的人,分享就是學(xué)習(xí)的一種樂(lè)趣
* 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);
}
}
/**
* 通過(guò)反射的方式獲取狀態(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è)是隱藏的布局,然后通過(guò)動(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>
接下來(lái)看下第三種
這個(gè)是用的github上的第三方庫(kù)
1.庫(kù)地址:https://github.com/jgilfelt/SystemBarTint
2.添加依賴(lài)庫(kù):
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);
來(lái)看下代碼吧
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è)懂得了編程樂(lè)趣的小白,希望自己
* 能夠在這個(gè)道路上走的很遠(yuǎn),也希望自己學(xué)習(xí)到的
* 知識(shí)可以幫助更多的人,分享就是學(xué)習(xí)的一種樂(lè)趣
* 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>
好了,原來(lái)自己以為沉浸式狀態(tài)欄聽(tīng)著好厲害(有可能自己原先不知道),但是真正自己去做了,去了解了,也沒(méi)有那么難、那么神秘了,我想這也是自己成長(zhǎng)了一些。
繼續(xù)努力。這個(gè)是上傳的github上的demo: https://github.com/wuyinlei/-
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android操作XML數(shù)據(jù)技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android開(kāi)發(fā)入門(mén)與進(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)欄和華為虛擬按鍵沖突問(wèn)題
- Android 沉浸式狀態(tài)欄與隱藏導(dǎo)航欄實(shí)例詳解
- Android 詳解沉浸式狀態(tài)欄的實(shí)現(xiàn)流程
相關(guān)文章
Android金額輸入框只允許輸入小數(shù)點(diǎn)后兩位效果
實(shí)現(xiàn)android 金額輸入框輸入小數(shù)點(diǎn)后兩位的效果也不是很復(fù)雜,只需要設(shè)置輸入框輸入的字符類(lèi)型、設(shè)置InputFilter、設(shè)置輸入變化監(jiān)聽(tīng)即可。這篇文章主要介紹了Android金額輸入框只允許輸入小數(shù)點(diǎn)后兩位 ,需要的朋友可以參考下2017-05-05
Android實(shí)現(xiàn)九宮格(GridView中各項(xiàng)平分空間)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)九宮格(GridView中各項(xiàng)平分空間)的方法,涉及Android針對(duì)GridView操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Android網(wǎng)絡(luò)編程之UDP通信模型實(shí)例
這篇文章主要介紹了Android網(wǎng)絡(luò)編程之UDP通信模型實(shí)例,本文給出了服務(wù)端代碼和客戶(hù)端代碼,需要的朋友可以參考下2014-10-10
Android Studio做超好玩的拼圖游戲 附送詳細(xì)注釋源碼
這篇文章主要介紹了用Android Studio做的一個(gè)超好玩的拼圖游戲,你是0基礎(chǔ)Android小白也能包你學(xué)會(huì),另外附送超詳細(xì)注釋的源碼,建議收藏!2021-08-08
Android給TextView添加點(diǎn)擊事件的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇Android給TextView添加點(diǎn)擊事件的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
Android實(shí)現(xiàn)listview動(dòng)態(tài)加載數(shù)據(jù)分頁(yè)的兩種方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)listview動(dòng)態(tài)加載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android獲取所在時(shí)區(qū)時(shí)間的兩種方式
Android獲取所在時(shí)區(qū)正確時(shí)間的方式有兩種,通過(guò)wifi獲取時(shí)間和通過(guò)通過(guò)GPS獲取時(shí)間這兩種方式,文中通過(guò)代碼示例給大家的介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04

