提问人:Henrique Branco 提问时间:8/20/2023 最后编辑:Henrique Branco 更新时间:10/16/2023 访问量:147
无法正确隐藏 ActionBar Android Studio Giraffe
Can't hide ActionBar Android Studio Giraffe properly
问:
我正在使用 Android Studio Giraffe |2022.3.1 并且我无法正确隐藏 ActionBar。
我看到了这个和这个相关的问题,但它们对我没有帮助。
这是我的 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){
}
}
}
同样在主题中.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 仍然存在:
为什么我根本无法隐藏它?
答:
0赞
MEGHA RAMOLIYA
9/27/2023
#1
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>
隐藏特定活动的操作栏的第二种方法:
<activity android:name=".activity.HomeScreen" android:theme="@style/Theme.AppCompat.Light.NoActionBar" android:screenOrientation="portrait" />
3.第三种方法是将此代码添加到kotlin文件中
supportActionBar?.hide()
- 使用窗口管理器隐藏操作栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
0赞
shembo
10/9/2023
#2
替换主题中的此行:.xml
<style name="Base.Theme.Motivation" parent="Theme.Material3.DayNight.NoActionBar">
用线
<style name="Base.Theme.Motivation" parent="Theme.MaterialComponents.DayNight.DarkActionBar" >
在此之后,可以使用以下命令隐藏 ActionBarsupportActionBar?.hide()
评论