Android區分沉浸式狀态欄與變色狀态欄等詳解我就不說了 網上描述太多了,可以參考:https://blog.csdn.net/u011835956/article/details/50997492,2.http://www.androidchina.net/3520.html,3.https://www.cnblogs.com/neillee/p/5402091.html,接下來我們說一下狀态欄背景如何設定成背景圖檔,效果圖如下:
Toolbar設定底色背景圖檔實作 步驟
1.建立toolbar_head_img.xml檔案,如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/toolbar_img"
android:layout_alignTop="@+id/toptop"
android:scaleType="fitXY"
android:src="@mipmap/bg_navigationbar" />
<LinearLayout
android:id="@+id/toptop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:orientation="horizontal" />
<TextView
android:id="@+id/toolbar_img_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toptop"
android:gravity="center"
android:paddingBottom="@dimen/ToolBarBackTextPadiing"
android:paddingTop="@dimen/ToolBarBackTextPadiing"
android:text="标題"
android:textColor="@color/white"
android:textSize="@dimen/ToolBarMainTextSize" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/toolbar_img_title"
android:layout_alignTop="@+id/toolbar_img_title">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" "
android:textSize="@dimen/ToolBarMainTextSize" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
2.values-->styles配置,代碼如下:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="Theme.mainTransparent" parent="AppTheme">
<!--在Android 4.4之前的版本上運作,直接跟随系統主題-->
<!--全透明效果-->
</style>
<!-- 圖檔全屏-->
<style name="Theme.imgFullScreen" parent="Theme.AppCompat.Light.NoActionBar">
<!--在Android 4.4之前的版本上運作,直接跟随系統主題-->
<!--全透明效果-->
</style>
</resources>
3.values-v19配置,代碼如下:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.mainTransparent" parent="AppTheme">
<!-- Customize your theme here. -->
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--全透明效果-->
</style>
<!-- 圖檔全屏-->
<style name="Theme.imgFullScreen" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--底部黑色,狀态欄淺v黑色-->
</style>
</resources>
4.values-v21配置,代碼如下:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.mainTransparent" parent="AppTheme">
<!-- Customize your theme here. -->
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">@color/transparent</item>
<!--全透明效果-->
</style>
<!-- 圖檔全屏-->
<style name="Theme.imgFullScreen" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">@color/transparent</item>
<!--底部黑色,狀态欄透明-->
</style>
</resources>
5.對應的activity引入該布局,代碼如下:
<include layout="@layout/toolbar_head_img"/>
6.在activity中需要這樣處理,代碼如下:
/**
* android4.3以上的沉浸式 ,4.3以下沒效果,是以不要頭部填充狀态欄高度
*/
int sysVersion = Build.VERSION.SDK_INT;
if (sysVersion > Build.VERSION_CODES.JELLY_BEAN_MR2 ) {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
LinearLayout toptop = (LinearLayout) this.findViewById(R.id.toptop);
RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams(this.getWindowManager().getDefaultDisplay().getWidth(), result);
//設定修改後的布局。
toptop.setLayoutParams(para);
}
initView();
如果在相應的的activity中加入上面這段代碼,我們導航會正确的顯示,效果圖如下:
如果不加入上面這段代碼,顯示效果如下:
另外一種設定:圖檔全屏顯示
在AndroidManifest.xml中我們是通過設定這樣的主題實作,具體設定如下代碼;
<activity
android:name=".FullScreenActivity"
android:theme="@style/Theme.imgFullScreen">
</activity>
體外話題,如果手機底部有虛拟鍵,比如華為的手機。我們在開發時,有時候遇到,我們的布局被虛拟鍵擋住了,我們該如何解決,直接有效的辦法,就是在我們的布局最底層加一個控件,比如是LinearLayout,然後我們在代碼中做這樣的處理:
/**
* 底部如果有靜态欄,往上托
*/
private void bottomStatusBar() {
//設定螢幕點選高度
int totalHeight = GetScreenSize.getDpi(this);
int contentHeight = GetScreenSize.getScreenHeight(this);
int bottom = totalHeight - contentHeight;
if (bottom > 0) {
RelativeLayout.LayoutParams rh = (RelativeLayout.LayoutParams) mainBottom.getLayoutParams();
rh.height = bottom;
mainBottom.setLayoutParams(rh);
ll_gbBootom.setPadding(0, 0, 0, rh.height);
}
}
mainBottom是我們的底部控件,ll_gbBootom在這裡是他的父控件,這裡我用的是:
<com.artmofang.utils.CourseScrollView
android:id="@+id/lt_ScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_gbBootom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
GetScreenSize工具utils:
package com.artmofang.utils;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import java.lang.reflect.Method;
/**
* Created by LT on 2017/2/6.
* 擷取手機螢幕的高,寬,原始尺寸包括虛拟鍵位等
*/
public class GetScreenSize {
//擷取螢幕尺寸寬
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
//擷取螢幕尺寸高度
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}
//擷取螢幕原始尺寸高度,包括虛拟功能鍵高度
public static int getDpi(Context context) {
int dpi = 0;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
@SuppressWarnings("rawtypes")
Class c;
try {
c = Class.forName("android.view.Display");
@SuppressWarnings("unchecked")
Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, displayMetrics);
dpi = displayMetrics.heightPixels;
} catch (Exception e) {
e.printStackTrace();
}
return dpi;
}
}