安卓全屏设置实现(基于Activity与AppCompatActivity)

为了让新手少走点弯路,现在新建的项目多大自动继承自AppCompatActivity。
如果采用以前的实现方法,都会出现报错的情况,很多博客只讲其一不讲其二
我在这里 两种方法都会提到

Activity下的实现
以下方法只能基于Activity,继承AppCompatActivity则不能实现。
一 . 通过代码
在setContentView之前执行:

requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏

二. 调用Android自带的Theme
直接在AndroidManifest.xml中需要全屏显示的Activity属性中添加

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

三 . 自己定义全屏Theme
在style.xml文件中定义theme(如果没有style.xml,在res/values目录下创建)

<resources> 
    <style name="Theme.NoTitle_FullScreen"> 
        <item name="android:windowNoTitle">true</item>    
        <item name="android:windowFullscreen">true</item>      
    </style> 
</resources>

直接在AndroidManifest.xml中需要全屏显示的Activity属性中添加

android:theme="@style/Theme.NoTitle_FullScree"





AppCompatActivity下的实现
* 一. 自己定义全屏Theme*

<style name="MyStyle" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>



二. 自己定义全屏Theme
在上Activity上提到的方法一与方法二在AppCompatActivity中是会报错的
方法三在AppCompatActivity中是不会报错的,但并不会真正实现全屏
会存在ActionBar无法隐藏的情况

一 . 基于上面Activity上的方法三,隐藏ActionBar
我们可以通过代码隐藏ActionBar , 记得初始化ActionBar 直接获取会报空指针

 getSupportActionBar().Hide();

二 . 自己定义全屏Theme
这里跟上面的方法三类似

   <style name="AppTheme.FullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowActionBar">false</item>
    </style>

猜你喜欢

转载自blog.csdn.net/qq_29983773/article/details/73472424