Android全屏,隐藏状态栏和标题栏

Android全屏,隐藏状态栏和标题栏,本来以为这个功能很简单,不必拿出来说,但这个问题让我在互联网上找了N多例子,花了数个小时,看来还是有必要好好说说的。

网络上的许多方案都是正确的,但要放到合适的环境里才起作用。出现了下面这几种情况。

1、看第一个出问题的,直接使用android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen",但等我的是一个错误

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jianchi.fsp.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
错误信息如下

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

看到错误了,看出来了,说让我们使用Theme.AppCompat theme,看来Theme.Black.NoTitleBar.Fullscreen在继承了AppCompatActivity的Activyty中会出问题。

2、将上面的代码都删除了,然后就代码来隐藏状态栏和标题栏。代码如下

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        //定义全屏参数
        int flag=WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        
        setContentView(R.layout.activity_main);
    }
}

运行一下,状态栏真的没有了,但标题栏仍然在哪里。为什么标题栏还在哪里?肯定有些情况下是可以的!不再研究它了。 ActionBar没消失

又查到下面的代码

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
终于标题栏ActionBar消失了。

推荐方法:在自定义的styles中设置隐藏状态栏和标题栏的theme,并在AndroidManifest.xml文件中的application或某个activity中设置。

styles文件如下

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoBar">
        <item name="windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>

</resources>
AndroidManifest.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jianchi.fsp.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" android:theme="@style/AppTheme.NoBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

实现状态栏和标题栏终于消失了,效果如下图



我建了一个Git仓库,存放我尝试过的例子,完整 代码都放在那里。

https://code.csdn.net/do168/androidtestcenter

欢迎下载


  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android中的状态栏是显示在设备屏幕顶部的区域,用于显示系统的通知和其他信息。如果想要在Android应用中去掉状态栏,可以通过使用Fragment来实现。 首先,在MainActivity的onCreate方法中,可以通过调用Window类的setFlags方法来隐藏状态栏。代码如下: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题栏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 隐藏状态栏 setContentView(R.layout.activity_main); } ``` 接下来,在Fragment的onCreateView方法中,将顶部布局的padding设置为0,以充满整个屏幕。代码如下: ```java @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_layout, container, false); View topLayout = view.findViewById(R.id.top_layout); topLayout.setPadding(0, 0, 0, 0); return view; } ``` 在上述代码中,R.layout.fragment_layout是Fragment的布局文件,需要根据实际情况进行替换。 最后,在AndroidManifest.xml文件中将MainActivity的theme设置为全屏模式。代码如下: ```xml <activity android:name=".MainActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> ... </activity> ``` 通过以上步骤,可以在Android应用中去掉状态栏。但请注意,由于各个手机厂商定制的系统的差异,可能无法在所有设备上完全去掉状态栏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值