一、隐藏标题栏
总体来说,Android隐藏标题栏分两种方式:onCreate中使用代码隐藏 和 AndroidMainfest 定义Activity主题隐藏 ;
Activity继承自Activity时,
onCreate-->setContentView方法前加入以下代码:
requestWindowFeature(Window.FEATURE_NO_TITLE); (也可用下方第二种方式)
Activity继承自AppCompatActivity时,
第一种方式:onCreate方法中隐藏
import android.support.v7.app.ActionBar;
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
第二种方式:AndroidManifest文件中改变Activity的主题(theme) (这种方式,继承自Activity或者AppCompatActivity都可用)
在res--->values--->styles--->中加入以下代码:
<style name="MainStyle" parent="Theme.AppCompat.Light.NoActionBar"/>
然后在需要隐藏标题栏的activity标签声明中,加入以下代码即可;
android:theme="@style/MainStyle"
二、隐藏状态栏
第一步:AndroidManifest文件中设置主题为NoActionBar 或者setContentView之前使用requestWindow设置无标题
第二步:在Activity的onCreate()方法中加入以下代码,在setContentView之后加入以下代码即可
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
第三步:在Activity的布局文件根标签下加入以下代码
android:fitsSystemWindows="true"
第四步:如果展示app启动图时,进入页面后白屏闪烁才可以看到启动页图片,可以在当前Activity的Theme中加入以下代码:
<style name="LauncherTheme" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">
<item name="android:windowBackground">@mipmap/ic_appstart</item>
</style>
附:
Android:theme="@android:style/Theme.Dialog" 将一个Activity显示为能话框模式
android:theme="@android:style/Theme.NoTitleBar" 不显示应用程序标题栏
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不显示应用程序标题栏,并全屏
android:theme="Theme.Light" 背景为白色
android:theme="Theme.Light.NoTitleBar" 白色背景并无标题栏
android:theme="Theme.Light.NoTitleBar.Fullscreen" 白色背景,无标题栏,全屏
android:theme="Theme.Black" 背景黑色
android:theme="Theme.Black.NoTitleBar" 黑色背景并无标题栏
android:theme="Theme.Black.NoTitleBar.Fullscreen" 黑色背景,无标题栏,全屏
android:theme="Theme.Wallpaper" 用系统桌面为应用程序背景
android:theme="Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景,且无标题栏
android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景,无标题栏,全屏
android:theme="Translucent" 透明背景
android:theme="Theme.Translucent.NoTitleBar" 透明背景并无标题
android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 透明背景并无标题,全屏
android:theme="Theme.Panel" 面板风格显示
android:theme="Theme.Light.Panel" 平板风格显示