沉浸式狀態(tài)欄是安卓4.4以后才出現(xiàn)的,昨天閑的無聊,想提升自己應(yīng)用的逼格。于是研究了下沉浸式,看了很多篇博客,有多種方法,最后自己總結(jié)了下,改動下適合自己的。
然后封了工具類(最后附上完整代碼)。
實現(xiàn)思路: 1,判斷當(dāng)前版本是不是4.4以上。因為4.4之前是沒有這功能的。
2,設(shè)置狀態(tài)欄為透明。
mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
3,一般設(shè)置透明之后,我們的標(biāo)題欄會占用了狀態(tài)欄的空間,像這樣。
4,這里就關(guān)鍵了,網(wǎng)上說在根布局設(shè)置Android:fitsSystemWindows="true",這樣的確可以解決,但是需要每個布局都這樣設(shè)置有寫麻煩,(有說在theam中設(shè)置能一次解決,親測沒用)。
這里說下我的方法:
1、動態(tài)的獲取statusbar的高度,然后給自己的標(biāo)題欄重新設(shè)置高度,加上了statusbar的高度。并且設(shè)置他的pandinTOP。像這樣。
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)ll_title.getLayoutParams();
params.height+= statusHeight;
ll_title.setBackgroundResource(R.color.*apply_state_orange*);//這里設(shè)置狀態(tài)欄的顏色,與標(biāo)題欄一致就好
ll_title.setLayoutParams(params);//動態(tài)的設(shè)置標(biāo)題欄的panddingTop----不設(shè)置標(biāo)題欄的內(nèi)容還是會往狀態(tài)欄擠
ll_title.setPadding(0,statusHeight,0,0);
有關(guān)于這個title講一下,這是我標(biāo)題欄的根布局的id,后面會附上布局和完整代碼,
接下來看效果圖
沉浸式
非沉浸式
代碼實現(xiàn)--核心類---可直接copy
注釋很詳細,絕對一看就懂。
package com.example.nick.jiujiang_culture.util;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import com.example.nick.jiujiang_culture.R;
import java.lang.reflect.Field;
/**
* Created by Nick on 2017/3/21.
* 沉浸式狀態(tài)欄工具類(其實叫透明)
*/
public class ImmersionUtil {
private Activity mActivity;
private View mView;
private boolean flag;//啟用還是關(guān)閉
//null說明是acitivity.不為空則是fragment;
//這邊如果所有的fragment的title布局都一樣的話可以直接放activtity,也就不用判斷了
private static ImmersionUtil immersionUtil;
LinearLayout ll_title;//標(biāo)題欄
public ImmersionUtil(Activity activity, View view){
mActivity=activity;
mView=view;
}
public static ImmersionUtil newIntance(Activity activity, View view){
immersionUtil=new ImmersionUtil(activity,view);
return immersionUtil;
}
public void initState(){
// flag=true;
//啟用/關(guān)閉
if (!flag)
return;
//判斷當(dāng)前版本是否大于4.4,因為沉浸式這個功能是在這之后的
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
//判斷的當(dāng)前界面是activity還是fragment
if (mView!=null){
ll_title= (LinearLayout) mView.findViewById(R.id.ll_title);
}else {
ll_title= (LinearLayout) mActivity.findViewById(R.id.ll_title);
}
if (ll_title!=null){
int statusHeight=getStatusHeight();//獲取狀態(tài)欄高度
if (statusHeight>0){
//透明狀態(tài)欄
mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明導(dǎo)航欄
// mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
ViewGroup.LayoutParams params= ll_title.getLayoutParams();
params.height+=statusHeight;//動態(tài)的設(shè)置標(biāo)題欄的高度。
//動態(tài)的設(shè)置標(biāo)題欄的panddingTop----不設(shè)置標(biāo)題欄的內(nèi)容還是會往狀態(tài)欄擠
ll_title.setPadding(0, statusHeight, 0, 0);
ll_title.setLayoutParams(params);
ll_title.setBackgroundResource(R.color.colorAccent);//這里設(shè)置狀態(tài)欄的顏色,與標(biāo)題欄一致就好
}
}
}
}
//如果是要背景圖填充的話需要5.0以上
public void initStateImager(){
//判斷當(dāng)前版本是否大于5.0,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View decorView = mActivity.getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
mActivity.getWindow().setStatusBarColor(Color.TRANSPARENT);//其實也就是這個方法需要5.0以上
}
}
//通過反射獲取status高度
private int getStatusHeight() {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");//得到dimen這個類,
Object object = c.newInstance();//創(chuàng)建一個實例來代替dimen
Field field = c.getField("status_bar_height");//得到status_bar_height
int x = Integer.parseInt(field.get(object).toString());
return mActivity.getResources().getDimensionPixelSize(x);
}
catch (Exception e){
e.printStackTrace();
}
return 0;
}
}
最后:在自己的基類activity和基類fragment中調(diào)用這個方法。
這樣其他的activity和fragment繼承這兩個基類就可以了。
博主新手,只為記錄自己的成長。當(dāng)然能幫到其他人更好。
參考http://blog.csdn.NET/wuyinlei/article/details/50564274