【问题标题】:Change the style of ActionBar更改 ActionBar 的样式
【发布时间】:2015-11-12 00:53:47
【问题描述】:

我正在处理我的操作栏,我正在尝试更改样式,我想要背景为白色,文本颜色为蓝色,我对 style.xml 执行以下操作:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/action_bar</item>
    <item name="colorAccent">@color/top_bar</item>
    <item name="colorPrimaryDark">@color/cyan_500</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" 
                 parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="android:actionBarSize">20dp</item>
    <item name="android:background">@color/white</item>
    <item name="android:titleTextStyle">@style/MyTitleTextBarStyle</item>
</style>

<style name="MyTitleTextBarStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/colorRB</item>
</style>

</resources>

对于 API 15 到 22,什么都没有发生。 我已经设法通过这样做的代码来改变背景:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reasonlocation);
    android.support.v7.app.ActionBar mAb = getSupportActionBar();
    mAb.setTitle(getString(R.string.reasonandlocation));
    mCd.setColor(getResources().getColor(R.color.white));
    mAb.setBackgroundDrawable(mCd);
    ...
}

如果您知道如何通过 style.xml 或代码解决此问题,我将不胜感激。

【问题讨论】:

    标签: android android-actionbar android-appcompat


    【解决方案1】:

    您可以通过创建自定义样式来定义 ActionBar(和其他东西)的颜色:

    只需编辑您的 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>
    

    然后将“MyCustomTheme”设置为包含 ActionBar 的 Activity 的主题。

    您还可以像这样为 ActionBar 设置颜色:

      ActionBar actionBar = getActionBar();
     actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED)); // set your desired color
    

    【讨论】:

    • 这正是我所说的......对我不起作用,知道为什么吗?
    • 尝试改变这部分 parent="Theme.AppCompat.Light"
    【解决方案2】:

    我通过代码解决了它,将以下内容添加到我之前的 onCreate 代码中

        Spannable text = new SpannableString(mAb.getTitle());
        text.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorRB)), 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        mAb.setTitle(text);
    

    【讨论】:

      【解决方案3】:

      android:actionBarStyle 中删除android: 平台相关前缀。

      【讨论】:

        猜你喜欢
        • 2013-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多