【发布时间】:2014-07-01 15:38:21
【问题描述】:
如果我按下操作栏中的按钮,那么它的背景颜色不是我想要的。我的项目的背景颜色不响应我的点击事件。如何更改它并在按下时更改背景颜色?
【问题讨论】:
-
您必须自定义操作栏。看看这两个链接Customizing ActionBarCustom ActionBar
标签: android button colors background android-actionbar
如果我按下操作栏中的按钮,那么它的背景颜色不是我想要的。我的项目的背景颜色不响应我的点击事件。如何更改它并在按下时更改背景颜色?
【问题讨论】:
标签: android button colors background android-actionbar
您需要声明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 中,您可以找到所有可能的自定义选项和所有属性。
【讨论】:
android:actionBarItemBackground 被Android 理解,而actionBarItemBackground 给出错误,所以我删除了它,但有必要两者兼而有之吗?
actionBarItemBackground 在编译时出错,所以我将其删除。在这种情况下,我该如何同时使用。
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0a0a0a")));
这可能会有所帮助
【讨论】: