无法正确隐藏ActionBar Android Studio Giraffe

问题描述 投票:0回答:1

我正在使用 Android Studio Giraffe | 2022.3.1,我无法正确隐藏 ActionBar。

我看到了thisthis相关的问题,但它们没有帮助我。

这是我的 MainActivity.kt 文件:

package com.example.motivation

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.example.motivation.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)

        // Hide ActionBar
        supportActionBar?.hide()

        // Events
        binding.buttonNewPhrase.setOnClickListener(this)

    }

    override fun onClick(view: View) {
        if (view.id == R.id.button_new_phrase){
        }
    }
}

另外在themes.xml默认没有ActionBar,没改过

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.Motivation" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
    </style>

    <style name="Theme.Motivation" parent="Base.Theme.Motivation" />
</resources>

这是 AndroidManifest.xml:

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Motivation"
        tools:targetApi="31">
        <activity
            android:name=".UserActivity"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
         </activity>
        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

</manifest>

当我执行应用程序时,ActionBar 仍然存在:

为什么我根本无法隐藏它?

android kotlin mobile android-actionbar
1个回答
0
投票

1.第一种隐藏ActionBar的方法:

         1.1 Hide Action Bar permanently from theme.xml file
        
        <resources >
            <!-- Base application theme. -->
        
            <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
                <!-- Customize your theme here. -->
                <item name="colorPrimary">@color/colorPrimary</item>
                <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
                <item name="colorAccent">@color/colorAccent</item>
                <item name="android:windowBackground">@android:color/transparent</item>
            </style>
        
         1.2 Add theme in AndroidManifest.xml file
        
        <application
                android:allowBackup="true"
                android:dataExtractionRules="@xml/data_extraction_rules"
                android:fullBackupContent="@xml/backup_rules"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"
                android:roundIcon="@mipmap/ic_launcher_round"
                android:supportsRtl="true"
                android:theme="@style/AppTheme"
                tools:targetApi="31">
        ......
        </application>
        
  1. 隐藏特定活动的操作栏的第二种方法:

      <activity android:name=".activity.HomeScreen"
      android:theme="@style/Theme.AppCompat.Light.NoActionBar"
      android:screenOrientation="portrait" />
    

    3.第三种方法是在kotlin文件中添加此代码

      supportActionBar?.hide()
    
    1. 使用窗口管理器隐藏操作栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
© www.soinside.com 2019 - 2024. All rights reserved.