【问题标题】:Unable to Style ActionBar Android AppCompat无法设置 ActionBar Android AppCompat 的样式
【发布时间】:2015-01-31 12:01:19
【问题描述】:

我已将我的主题包含在 Manifest 文件中。我的主题代码如下

<resources>
    <!-- Activity themes -->
    <style name="Theme.Base" parent="Theme.AppCompat.Light" />

    <style name="Theme.Sample" parent="Theme.Base" />

    <style name="AppTheme" parent="Theme.Sample" >
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
        <item name="android:background">@color/blue</item>
    </style>
</resources>

我所有的活动都继承ActionBarActivity

我似乎无法更改ActionBar 的颜色。 请帮忙。 我正在使用 API 21 的模拟器上运行它。
如何设置我的 ActionBar 样式?

【问题讨论】:

    标签: android android-actionbar styles android-appcompat


    【解决方案1】:

    AppCompat v21 不读取任何与操作栏相关的 android: 属性:

    <style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
        <!-- Here we setting appcompat’s actionBarStyle -->
        <item name="actionBarStyle">@style/MyActionBarStyle</item>
    
        <!-- ...and here we setting appcompat’s color theming attrs -->
        <item name="colorPrimary">@color/my_awesome_red</item>
        <item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
    
        <!-- The rest of your attributes -->
    </style>
    

    【讨论】:

      【解决方案2】:

      您可以更改颜色,将此代码添加到您的样式中:

      <item name="colorPrimary">@android:color/holo_red_dark</item>
      

      使用 colorPrimaryDark 您可以更改状态栏颜色。

      小心,使用 colorPrimary,不要使用 android:colorPrimary,这最后只适用于 21。

      所有代码:

      <resources>
      <!-- Activity themes -->
      <style name="Theme.Base" parent="Theme.AppCompat.Light" />
      
      <style name="Theme.Sample" parent="Theme.Base" />
      
      <style name="AppTheme" parent="Theme.Sample" >
          <item name="colorPrimary">@android:color/holo_red_dark</item>
      </style>    
      </resources>
      

      【讨论】:

        【解决方案3】:

        我发现这个用于 v21 appCompat 的 Google Blog Post 非常有用。它很好地解释了用法。

        【讨论】:

          【解决方案4】:

          您可以通过以下方式做到这一点

          只需编辑您的 Android 项目的 res/values/styles.xml 文件。

          例如这样:

          <resources>
              <style name="MyCustomTheme" parent="@android:style/Theme.Holo.Light">
                  <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
              </style>
          
              <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
                  <item name="android:background">ANY_HEX_COLOR_CODE</item>
              </style>
          </resources>
          

          或者通过java代码

          ActionBar actionBar = getActionBar();
          actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED));
          

          【讨论】:

          • 应用程序挂起。
          • 您必须使用 getSupportActionBar() 而不是 getActionBar()。 AppCompat 也适用于 Theme.AppCompat 样式,而不是 Holo 样式。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-15
          • 2012-03-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-16
          相关资源
          最近更新 更多