Android中实现全屏、无标题栏的两种办法(另附Android系统自带样式的解释)

实现全屏无标题栏:

1.在xml文件中进行配置

AndroidManifest.xml中,找到需要全屏或设置成无标题栏的Activity,在该Activity进行如下配置即可。

全屏效果:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  
无标题栏(但有系统状态栏):
android:theme="@android:style/Theme.NoTitleBar"  

2.编写代码设置
在程序中编写代码进行设置,只需在onCreate()方法中 setContentView方法之前加入如下代码:

// Full Screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

// No Titlebar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

可能出现的问题

Caused by: java.lang.IllegalStateException: You need to use a
Theme.AppCompat theme (or descendant) with this activity.

起因:在Manifest中设置activity全屏,代码如下:

<activity  
    android:name=".SplashActivity"  
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  
    android:label="@string/app_name" >  

原因: 从错误提示中提到Theme.AppCompat theme,这是因为我们的activity一定是继承了兼容包中的类,
比如我这里就无意中继承了AppCompatActivity,它来自android.support.v7.app.AppCompatActivity。
所以就要使用与其配合的AppCompat的theme才行。

解决:
方法1.根据提示来使用AppCompat的theme,如下:

<activity  
    android:name=".MainActivity"  
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"  
    android:label="@string/app_name" >  

在onCreate方法中

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 全屏状态(消除状态栏)+androidManifest.xml:android:theme="@style/AppTheme.NoActionBar"
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        handler = new Handler();
        handler.postDelayed(runnable, SPLASH_DISPLAY_LENGHT);
    }

方法2.如果不是那么强烈需要继承自AppCompatActivity,就直接继承Activity

附:Android系统自带样式

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”
平板风格显示

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值