【问题标题】:Display action bar in few activities在少数活动中显示操作栏
【发布时间】:2020-01-11 19:14:38
【问题描述】:

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

<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>

在我的清单中,我使用了这种风格:

<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">

这样我的所有活动中都没有操作栏。但是我希望它只出现在某些活动中。我尝试添加代码:

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()

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

【问题讨论】:

    标签: android kotlin android-actionbar


    【解决方案1】:

    使用操作栏创建样式

    <!-- 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 android:name=".MyActivity"
                android:theme="@style/AppTheme"
                android:label="MyActivity"></activity>
    

    没有操作栏的活动

    <activity android:name=".MyActivity"
                android:theme="@style/AppTheme.NoActionBar"
                android:label="MyActivity"></activity>
    

    【讨论】:

      【解决方案2】:

      为 With ActionBar 创建主题

      <style name="AppActionBarTheme" parent="Theme.AppCompat.Light">
      
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
      
      </style>
      

      你已经有主题 AppTheme 没有操作栏。

      像这样将这个主题添加到Manifest文件中的Activity中

      对于 NoActionBar

        <activity android:name=".NoActionBarActivity"
              android:theme="@style/AppTheme"
              android:label="NoActionBarActivity"></activity>
      

      对于操作栏

        <activity android:name=".ActionBarActivity"
              android:theme="@style/AppActionBarTheme"
              android:label="ActionBarActivity"></activity>
      

      对于整个应用程序。只需在应用程序标记中添加android:theme="@style/AppTheme" 行。就像这样

        <application
              android:theme="@style/AppTheme"/>
      

      或者像这样以编程方式隐藏/显示ActionBar

        //to show ActionBar
        supportActionBar?.show()
      
       //to hide ActionBar
        supportActionBar?.hide()
      

      【讨论】:

        猜你喜欢
        • 2015-08-07
        • 2014-10-12
        • 2018-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-04
        • 1970-01-01
        相关资源
        最近更新 更多