我创建了一个没有操作栏的自定义主题样式:
<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/