[英]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.