![](/img/trans.png)
[英]How to change the background color of an icon in the action bar, when clicked?
[英]How to make action bar icon change when clicked
我在SherlockActionBar中的紫色背景上有一组白色图标
我想让它们在点击时变成深紫色。
我有深紫色的相同图标,所以我想让那些drawable出现在按下状态。
现在,我知道如何在整个应用程序主题中执行此操作,但这意味着我必须对所有图标使用相同的drawable。
我想知道如何为每个项目的按下状态指定不同的drawable。
这是我现在使用的代码:
在styles.xml中
<style name="Theme.SherlockCustom" parent="@style/Theme.Sherlock.Light">
<item name="actionBarItemBackground">@drawable/action_bar_item_background</item>
<item name="android:icon">@android:color/transparent</item>
<item name="displayOptions">showCustom</item>
<item name="android:minWidth">0dp</item>
<item name="android:padding">0dp</item>
</style>
action_bar_item_background.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/light_purple" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="@android:color/transparent"/>
<!-- default -->
</selector>
这是不起作用的部分(尝试设置定义特定按钮的新外观的样式):
feed_icon.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/ic_action_content_paste"
android:right="@dimen/action_bar_icons_padding_right"
android:theme="@style/feediconstyle"></item>
</layer-list>
和style / feediconstyle:
<style name="feediconstyle" parent="@style/Theme.Sherlock.Light">
<item name="actionBarItemBackground">@drawable/feed_icon_background</item>
<item name="displayOptions">showCustom</item>
</style>
这个特殊的图标不符合新的风格
feed_icon_background:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/feed_icon_purple" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="@android:color/transparent"/>
<!-- default -->
</selector>
那我该怎么做?
我想符合条件“我还没有使用ActionBarSherlock仅检查标准操作栏”
这就是说我知道这将在标准的操作栏上工作,并且没有理由不在ActionBarSherlock中。
在我的活动的onCreate中(我正在使用logo属性,但它也适用于MenuItems)
getActionBar().setLogo(R.drawable.logo);
getActionBar().setDisplayUseLogoEnabled(true);
getActionBar().setHomeButtonEnabled(true);
在logo.xml中,我有一个选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/logopressed"/>
<item android:drawable="@drawable/logonotpressed" />
</selector>
在logopressed.xml中可以使用图层列表来创建背景
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@android:color/holo_purple" />
<item
android:drawable="@drawable/logo_normal" />
</layer-list>
在logonotpressed.xml中,根据需要切换背景颜色和/或图标drawable
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@android:color/white" />
<item
android:drawable="@drawable/logo_inverted" />
</layer-list>
所以这个解决方案的关键是设置选择器drawable并让它为每个状态选择一个全新的drawable,而不是将一个选择器设置为一个静态变量的background属性
注意:您应该能够使用背景来设置主题而不是2层,但我无法使其工作
我只是在ActionBar
使用自定义视图。 您可以通过覆盖Activity
onCreateOptionsMenu(Menu)
并将Button
设置为Action View来完成此操作 。 我还在NineOldAndroids
库中使用MenuItemCompat
来使用API 2.2(Froyo)和up:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
MenuItem feed = menu.findItem(R.id.feed);
Button feedButton = new Button(this);
//here set the custom statelist (selector)
feedButton.setBackgroundResource(R.drawable.feed_icon_background);
MenuItemCompat.setActionView(feed, feedButton);
//ensure the clicks are handled correctly in the Activity
feedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(feed);
}
});
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.