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;
}
}
domeo下载https://github.com/yangxiansheng123/ToolbarSetBg