value-v19
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!-- Base application theme. --> <style name="AppTheme" parent="BaseTheme"> <item name="windowNoTitle">true</item> <item name="android:windowNoTitle">true</item> <!--<item name="android:windowFullscreen">true</item>--> <item name="android:windowIsTranslucent">true</item> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">false</item> </style>
value-v21
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!-- Base application theme. --> <style name="AppTheme" parent="BaseTheme"> <item name="windowNoTitle">true</item> <item name="android:windowNoTitle">true</item> <!-- <item name="android:windowFullscreen">true</item>--> <item name="android:windowIsTranslucent">true</item> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">false</item> <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色--> <item name="android:statusBarColor">@android:color/transparent</item> </style>
6.0以上(23)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //6.0上设置透明状态栏 Window win = this.getWindow(); win.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//透明状态栏 // 状态栏字体设置为深色,SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 为SDK23增加 win.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); // 部分机型的statusbar会有半透明的黑色背景 win.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); win.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); win.setStatusBarColor(Color.TRANSPARENT);// SDK21 }
7.0以上(24)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0及以上状态栏透明设置 try { Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView"); Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor"); field.setAccessible(true); field.setInt(getWindow().getDecorView(), Color.TRANSPARENT); //改为透明 } catch (Exception e) { e.printStackTrace(); } }
需要在 setContentView之前设置
以下为例
代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.young.demostatus.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:paddingTop="20dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:text="标题" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="@color/colorAccent" android:hint="这是一个输入框" /> </RelativeLayout> </LinearLayout>
activity需要设置以下模式
android:windowSoftInputMode="adjustResize"
以上白色代码段为防止键盘挡住输入框