【问题标题】:Android: Problems with ActionBar CustomizationAndroid:ActionBar 自定义的问题
【发布时间】:2015-06-20 14:49:03
【问题描述】:

我刚刚开始自学 Android 开发,遇到了问题,需要帮助。

我正在了解ActionBar。我有兴趣自定义ActionBar

我更改其背景颜色和项目间距的步骤是:

  1. 这个想法是有一个带有ActionBar 的基本主题。然后自定义它。代码:

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
    </style>
    
    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#F44336</item>
    </style>
    

  2. 接下来,在我的AndroidManifest.xml 中,我使用我创建的自定义主题。代码:

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    

但是我的ActionBar 的颜色还是黑色。我究竟做错了什么?我错过了什么吗?

【问题讨论】:

    标签: android android-layout android-actionbar android-theme


    【解决方案1】:

    您正在使用appcompat-v7 操作栏反向移植。要设置操作栏的颜色,请在您的主题中使用colorPrimary,而不是android:background

    例如,这个主题设置了三种主要的色调:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <style name="Theme.Apptheme" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>
      </style>
    </resources>
    

    这个特定的示例恰好通过颜色资源在res/values/colors.xml 文件中定义了颜色值:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <color name="primary">#3f51b5</color>
      <color name="primary_dark">#1a237e</color>
      <color name="accent">#ffee58</color>
    </resources>
    

    使用自定义主题的活动然后将主颜色作为操作栏背景:

    (来自this sample project

    【讨论】:

    • 谢谢。我还有一个问题。这将帮助我控制颜色,但是如何控制 ActionBar 的其他属性。比如图标之间的距离?
    • @HaggarTheHorrible:理想情况下,您只保留图标之间的距离。不过,除此之外,“其他属性”由主题设置或通过调用从 getSupportActionBar() 获得的 ActionBar 控制。
    猜你喜欢
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 2022-01-21
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多