【问题标题】:Change actionbar button background color when pressed按下时更改操作栏按钮的背景颜色
【发布时间】:2014-07-01 15:38:21
【问题描述】:

如果我按下操作栏中的按钮,那么它的背景颜色不是我想要的。我的项目的背景颜色不响应我的点击事件。如何更改它并在按下时更改背景颜色?

【问题讨论】:

标签: android button colors background android-actionbar


【解决方案1】:

您需要声明android:actionBarItemBackground 属性是:

操作栏项目的自定义项目状态列表可绘制背景。

然后,在您的样式中执行以下操作:

<style name="CustomStyle" parent="@style/Theme.Holo.Light" >
    <item name="android:actionBarItemBackground">@drawable/ab_item_background</item>
    <item name="actionBarItemBackground">@drawable/ab_item_background</item>
</style>  

因此,将您自己的可绘制对象与selector 和每个状态(按下、聚焦、禁用等)一起放入预期的背景。例如,上面声明的drawable ab_item_background.xml可能是这样的:

<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    <!-- focused/pressed: color=red -->
    <item 
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@color/red" />
    <!-- pressed: color=red -->
    <item 
        android:state_pressed="true"
        android:drawable="@color/red" />
    <!-- normal: color=transparent -->
    <item 
        android:drawable="@android:color/transparent" />
</selector>

Styling the Action Bar 中,您可以找到所有可能的自定义选项和所有属性。

【讨论】:

  • 使用 DarkActionBar 修复它
  • 工作完美,谢谢.. 虽然android:actionBarItemBackground 被Android 理解,而actionBarItemBackground 给出错误,所以我删除了它,但有必要两者兼而有之吗?
  • 这取决于您的操作栏@RohanKandwal。如果你使用 SherlockAB 或 Support 库,你必须使用它,是的。
  • @Fllo 我使用支持库,正如我所说,actionBarItemBackground 在编译时出错,所以我将其删除。在这种情况下,我该如何同时使用。
  • 我看到@RohanKandwal。当您在 style.xml 中编写这些属性时,您必须关闭此文件,清理并保存您的项目。
【解决方案2】:
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0a0a0a")));

这可能会有所帮助

【讨论】:

  • 这是更改操作栏的背景颜色,而不是主页按钮
猜你喜欢
  • 1970-01-01
  • 2017-02-04
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
相关资源
最近更新 更多