轉載請標明出處:http://www.lovecoder.cn/874.html;本文出自:【愛編程】
首先我們來看看網易云音樂對比圖:
對于成沉浸式狀態的版本支持:
Android在4.4版本推出一個透明狀態欄的概念,使手機頂部的狀態欄的顏色全透明。在5.0版本推出了Material Design,可以修改狀態欄顏色。所以4.4之前的版本是無法設置狀態欄顏色的。
具體實現:
在gradle文件中添加:
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:support-v4:22.2.1'
compile 'com.android.support:design:22.2.0'
注意:這里的版本根據自己的環境來修改.
(一)colors.xml 和 styles.xml
res/values/color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
下面定義幾個styles.xml
注意文件夾的路徑
values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- 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="MyTheme" parent="@style/AppTheme">
</style>
</resources>
values-v19/styles.xml
<resources>
<style name="MyTheme" parent="@style/AppTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
在19版本里就會執行這個styles樣式,因為4.4的版本還沒有提供屬性直接操作,所以這里可以使用。
android:windowTranslucentStatus=true來設置透明。
準準備工作都差不多了。
我們來寫一個Activity來看看。
1、activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
<TextView
android:id="@+id/id_tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:text="HelloWorld"
android:textSize="30sp" />
</RelativeLayout>
MainActivity.java文件
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lovecoder.testandroid5xcontrol">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/MyTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
非常簡單的做完準備工作,我們來看看效果,三個版本的對比:
5.0 ---- 4.4.4 ---- 4.0
5.0:可以很明顯的可以看出在5.0已經完美達到了效果;
4.4:但是在4.4上發現了什么?是不是內容也疊加到了狀態欄上,這是什么問題呢?
4.2:這個由于不支持狀態欄的更改,狀態欄就是黑色了。
這里改如何解決這個適配問題呢?這時候就需要運用一個新的屬性:
android:fitsSystemWindows="true"
主要作用是通過調整當前設置這個屬性的view的padding去為我們的status_bar留下空間。
修改后的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:fitsSystemWindows="true">
<TextView
android:id="@+id/id_tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:text="HelloWorld"
android:textSize="30sp" />
</RelativeLayout>
看效果:
5.0 4.4.4 4.0
是的,就是這樣一行代碼就解決了,但是現在大伙是不是發現了一個問題,在4.4的系統上頂部的顏色變成了白色,那我們在做項目時UI肯定會改成自己的風格啦,這個時候該怎么辦?
咱們這么看,4.4之后加入windowTranslucentStatus的屬性之后,也就是我們可以用到狀態欄的區域了。
既然我們可以用到這塊區域,那么我們只要在根布局去設置一個與狀態欄等高的View,設置背景色為我們期望的顏色就可以了。
于是有了以下的代碼:
public class StatusBarCompat {
private static final int INVALID_VAL = -1;
private static final int COLOR_DEFAULT = Color.parseColor("#20000000");
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void compat(Activity activity, int statusColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (statusColor != INVALID_VAL) {
activity.getWindow().setStatusBarColor(statusColor);
}
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
int color = COLOR_DEFAULT;
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
if (statusColor != INVALID_VAL) {
color = statusColor;
}
View statusBarView = new View(activity);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getStatusBarHeight(activity));
statusBarView.[setBackground](http://www.lovecoder.cn/tag/setbackground/)Color(color);
contentView.addView(statusBarView, lp);
}
}
public static void compat(Activity activity) {
compat(activity, INVALID_VAL);
}
public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
代碼的思路很簡單,根據Activity找到android.R.content,在其中添加一個View(高度為statusbarHeight,背景色為我們設置的顏色,默認為半透明的黑色)。
那么只需要在Activity里面去寫上:
StatusBarCompat.compat(this);
就可以了。
如果你希望自己設置狀態看顏色,那么就用這個方法:
StatusBarCompat.compat(this, getResources().getColor(R.color.colorAccent));
這樣的話我們就解決了4.4到5.x的適配問題,一行代碼解決,感覺還是不錯的。
看最后的效果:
5.0 4.4.4 4.0
ok,這樣完美的解決了。
轉載請說明出處:http://www.lovecoder.cn/874.html