android - 在少数 Activity 中显示操作栏

标签 android kotlin android-actionbar

我创建了一个没有操作栏的自定义主题样式:

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

在我的 list 中,我使用这种风格:
<application
    android:largeHeap="true"
    android:name="androidx.multidex.MultiDexApplication"
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

这样我的所有 Activity 中都没有操作栏。但是我希望它只出现在某些 Activity 中。我尝试添加代码:
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val layoutInflater: LayoutInflater = LayoutInflater.from(this)
    val view: View = layoutInflater.inflate(R.layout.activity_edit_profile, null)

    setContentView(view)
    supportActionBar?.show()

但是操作栏没有显示,我认为原因是主题不支持操作栏。我只想在少数 Activity 中显示操作栏,即便如此,我是否应该创建一个带有操作栏的主题并在大多数 Activity 中以编程方式隐藏它?

最佳答案

使用操作栏创建样式

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- 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.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

然后像这样使用

带操作栏的 Activity
<activity android:name=".MyActivity"
            android:theme="@style/AppTheme"
            android:label="MyActivity"></activity>

没有操作栏的 Activity
<activity android:name=".MyActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:label="MyActivity"></activity>

关于android - 在少数 Activity 中显示操作栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59693638/

相关文章:

kotlin - 如何通过 kotlinpoet 生成具有自定义接口(interface)的类

android - 如何在 ActionBar 中同时显示图标和 Action 标题?

java - 首选项屏幕以及半透明的导航和状态栏

android - 如何启用 Actionbar 文件夹功能,如新的 GMail 应用程序

android-volley - 启用 ProGuard 规则时 Gson 解析不起作用

android - AudioTrack - AudioTack 不播放完整录制的音频

android - 这个字段怎么注入(inject)github android?

android - 如何使用Android中的库所使用的库

android - 只能使用 releaseImplementation 和 debugImplementation 从 maven 添加 Kotlin Multiplatform Mobile 库

android - 透明的AppBarLayout,其中包含CardView