標籤:
android的狀態列(statusBar)版本的差異化比較大。在android 4.4 以上和5.x可以設定狀態列背景顏色,但是不可以設定狀態列中字和表徵圖的顏色。而系統預設的statusbar的字型和表徵圖顏色為白色。如果在6.0以下的要實現透明狀態列(也就是把整個介面延伸到statusbar),就要考慮到如果您的應用背景顏色為白色的時候,會出現statusbar裡的內容都看不清楚,這一點暫時是沒辦法去適配的。但是6.0以上的是既能修改statusbar的背景顏色,也可以修改statusbar的字型和表徵圖顏色(只能是黑色或是白色)。
實現透明狀態列有兩種方法:
1.通過toolbar去實現。然後在root xml裡配置:fitSystemWindows 就可以
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/transparent" android:theme="@style/ActionBarTheme" />
ActionBarTheme的style為:
<style name="ActionBarTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"> <item name="android:textColorPrimary">@android:color/white</item> <item name="android:windowTranslucentStatus" tools:targetApi="19">true</item> <item name="android:windowContentOverlay">@null</item> <item name="windowActionBar">false</item> <!-- Material Theme --> <item name="colorPrimary">@color/transparent</item> <item name="colorPrimaryDark">@color/transparent</item> <item name="android:statusBarColor" tools:targetApi="21">@color/transparent</item> <item name="android:windowDrawsSystemBarBackgrounds" tools:targetApi="21">true</item> </style>
在Activity裡加入:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
但是這樣設定後,會發現,有編輯框的介面,在manifest裡及時配置了adjustResize,鍵盤彈出後,也不會把介面上推,會導致鍵盤會覆蓋到編輯框(如果您應用的編輯框距離底部比較近),體驗很不好。這個時候,您需要的是在這個介面裡加入自己定義的view,同時去override fitSystemWindows方法以及omApplyWindowInsets方法。
public class SoftInputAdjustTopView extends RelativeLayout { private int[] mInsets = new int[4]; public SoftInputAdjustTopView(Context context) { super(context); } public SoftInputAdjustTopView(Context context, AttributeSet attrs) { super(context, attrs); } public SoftInputAdjustTopView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected final boolean fitSystemWindows(Rect insets) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { mInsets[0] = insets.left; mInsets[1] = insets.top; mInsets[2] = insets.right; insets.left = 0; insets.top = 0; insets.right = 0; } return super.fitSystemWindows(insets); } @Override public final WindowInsets onApplyWindowInsets(WindowInsets insets) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { mInsets[0] = insets.getSystemWindowInsetLeft(); mInsets[1] = insets.getSystemWindowInsetTop(); mInsets[2] = insets.getSystemWindowInsetRight(); return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0, insets.getSystemWindowInsetBottom())); } else { return insets; } }}
這樣就可以很好的實現適配問題。
android 透明狀態列方法及其適配鍵盤上推(一)