(解決)android不同版本的【沉浸式狀態列】(4.4/5.0/5.1/6.0)
阿新 • • 發佈:2019-02-18
介紹:
各個版本有略微的區別,下面我就根據自己的測試和除錯寫出對應的方法:
4.4以上(API>=19)的前提下,任選以下方法:
- 1.法1:只要呼叫一個方法,setContentView前:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- 2.法2:寫一個基類,然後對於想要沉浸的選單欄,繼承其即可。
public class ActivityBase extends Activity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//只對api19以上版本有效
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
//為狀態列著色
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
//狀態列顏色
tintManager.setStatusBarTintResource(Color.TRANSPARENT);
}
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
}
(PS:其中上面的“SystemBarTintManager 類”大家百度一下即可下載)
api >21(5.0以上的)
對於api >21(5.0以上的),如果用上述的方法,也能看到透明,但是有一個暗底,所以看起來不算完全的沉浸式,所以採用以下方法即可。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivitUtil = ActivitUtil.getActivitUtil();
mActivitUtil.addActivit(this);
//沉浸式狀態列在API>=19的前提下,只要呼叫一個方法
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//api 21(5.0)以上需要新增此判斷,可以不要上面那句
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_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(Color.TRANSPARENT);//6.0的真機上反而是用這句生效
window.setStatusBarColor(getResources().getColor(R.color.title_color));
}
//還要設定偏移,否則狀態列和內容重疊
View view = View.inflate(this, R.layout.activity_main, null);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout content = new LinearLayout(this);
content.setPadding(0,getStatusBarHeight(),0,0);
content.addView(ll,lp);
setContentView(content);
}
/**
* 獲取狀態列高度
*
* @return
*/
private int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
/**
* 狀態列新增到佈局中,發現不加也成功
*
*/
// private void addStatusBar(ViewGroup viewGroup) {
// mStatusBar = new View(this);
// LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
// ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight());
// mStatusBar.setLayoutParams(lp);
// mStatusBar.setBackgroundColor(Color.TRANSPARENT);
// viewGroup.addView(mStatusBar);
// }