Android开发中,有时候根据需求要隐藏标题栏、状态栏、导航栏或者全屏显示。看似很简单的功能,但是不注意使用环境,就会出现错误。
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
在AndroidManifest.xml中使用
android:theme="@android:style/Theme.Light.NoTitleBar"
或
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
来隐藏标题栏和状态栏的错误。
下面我们就来详细的分析下不同情景下,隐藏标题栏、状态栏、导航栏及全屏实现方法:
隐藏标题栏
①在代码里实现
- Activity继承 Activity:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
注意:一定要写在setContentView()方法之前
- Activity继承 AppCompatActivity:
getSupportActionBar().hide();// 隐藏ActionBar
② 在清单文件(AndroidManifest.xml中的application或者activity)里面实现
- Activity继承 Activity:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.Light.NoTitleBar">
<!--android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>-->
...
</application>
或
<activity android:name=".MainActivity"
android:theme="@android:style/Theme.Light.NoTitleBar"/>
<!--android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>-->
- Activity继承 AppCompatActivity:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
...
</application>
或
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
③ 在style.xml文件里定义,在AndroidManifest.xml中的application或者activity上引用
- Activity继承 Activity/AppCompatActivity:
style.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="notitle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
AndroidManifest.xml文件:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/notitle">
...
</application>
或
<activity android:name=".MainActivity"
android:theme="@style/notitle"/>
注意:
1)android:theme="@android:style/Theme.Light.NoTitleBar"代码如果在application中配置的话,整个APP的 Activity都只能是继承自Activity,不能是AppCompatActivity,因为AppCompatActivity下的 Theme只能是ActionBar样式的;如果只在某一个 Activity下配置,那么只要确保当前 Activity是继承Activity,而不是继承AppCompatActivity即可;
2)android:theme="@style/Theme.AppCompat.Light.NoActionBar"代码如果在application中配置的话,整个APP的Activity可以继承Activity或者AppCompatActivity;如果只在某一个 Activity下配置,当前 Activity可以继承Activity或者AppCompatActivity。
隐藏状态栏
① 在代码里实现
- Activity继承 Activity/AppCompatActivity:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
② 在清单文件(AndroidManifest.xml中的application或者activity)里面实现
- Activity继承 Activity:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen">
...
</application>
或
<activity android:name=".MainActivity"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"/>
③ 在style.xml文件里定义,在AndroidManifest.xml中的application或者activity上引用
style.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="notitle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
AndroidManifest.xml文件:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/fullScreen">
...
</application>
或
<activity android:name=".MainActivity"
android:theme="@style/fullScreen"/>
注意:
1)android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"代码如果在application中配置的话,整个APP的Activity都只能是继承自Activity,不能是AppCompatActivity,因为AppCompatActivity下的 Theme只能是ActionBar样式的;
2)代码如果只在某一个 Activity下配置,那么只要确保当前Activity是继承Activity,而不是继承AppCompatActivity即可。
隐藏导航栏
public static void hideNavigationBar(Activity activity) {
View decorView = activity.getWindow().getDecorView();
int option = SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_IMMERSIVE;
decorView.setSystemUiVisibility(option);
}
显示全屏
非沉浸式
(1)有虚拟按键,隐藏标题栏、状态栏和导航栏;
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏,继承 Activity
activity.getSupportActionBar().hide();// 隐藏ActionBar,继承 AppCompatActivity
public static void setFullScreen(Activity activity) {
int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE;
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
}
(2)无虚拟按键,隐藏标题栏和状态栏,若有虚拟按键,无法隐藏导航栏;
- ① 在代码里实现
Activity继承 Activity/AppCompatActivity:
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
activity.getSupportActionBar().hide();// 隐藏ActionBar
public static void setFullScreen(Activity activity) {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
- ② 在清单文件(AndroidManifest.xml中的application或者activity)里面实现
Activity继承 Activity:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen">
...
</application>
或
<activity android:name=".MainActivity"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"/>
- ③ 在style.xml文件里定义,在AndroidManifest.xml中的application或者activity上引用
Activity继承 Activity/AppCompatActivity:
style.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="notitle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
AndroidManifest.xml文件:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/fullScreen">
...
</application>
或
<activity android:name=".MainActivity"
android:theme="@style/fullScreen"/>
沉浸式
(1)全沉浸
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
activity.getSupportActionBar().hide();// 隐藏ActionBar
public static void setFullScreen(Activity activity) {
Window window = activity.getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
(2)半沉浸
- ① 在代码里实现
Activity继承 Activity/AppCompatActivity:
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
activity.getSupportActionBar().hide();// 隐藏ActionBar
public static void setFullScreen(Activity activity) {
Window window = activity.getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
- ② 在style.xml文件里定义,在AndroidManifest.xml中的application或者activity上引用
Activity继承 Activity/AppCompatActivity:
style.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="notitle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
注意:requires API level 19 (minSdkVersion 19),minSdkVersion 低于19会报错无法编译使用
AndroidManifest.xml文件:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/fullScreen">
...
</application>
或
<activity android:name=".MainActivity"
android:theme="@style/fullScreen"/>