我試圖改變我在對話框中顯示的ActionBar上的「向上」圖標的顏色。ActionBar「向上」按鈕顏色變化
基本上,我在ActionBar上有一個藍色的背景。一旦我點擊搜索圖標,我將背景更改爲灰色,這也是我希望我的向上箭頭也變成灰色(前一種情況下爲白色,背景爲藍色)。
下面是代碼當我改變搜索的背景點:
MenuItemCompat.setOnActionExpandListener(searchView,
new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
mIsSearchViewOpen = true;
ActionBar ab = ((MyActivity) getActivity()).getSupportActionBar();
//Change the ActionBar background color when SearchView is expanded
if (ab != null) {
ab.setDisplayShowHomeEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_gray_rect));
ab.setHomeAsUpIndicator(R.drawable.ic_back_arrow); //this doesn't work. its still white.
}
return true;
}
});
在上述情況下,如所預期的灰色背景施加但爲什麼未示出的返回箭頭?它仍然是白色的。
編輯:我注意到的一件事是,它不適用於該項目展開,但適用於搜索後崩潰。所以,當對話框打開時,它的白色。當我點擊搜索擴展它時,它仍然是白色的。但是當我碰到這個,搜索崩潰時,它變成了灰色。
編輯: 設置我的行動吧,我MyActivity
喜歡如下:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if(toolbar == null) {
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
ab.setElevation(0);
}
} else {
setSupportActionBar(toolbar);
}
而且,這裏是我的toolbar
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:contentInsetLeft="72dp"
android:contentInsetStart="72dp"
app:contentInsetLeft="72dp"
app:contentInsetStart="72dp"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:theme="@style/MyApp.ThemeOverlay.AppCompat.ActionBar"
app:titleTextAppearance="@style/TextAppearance.MyApp.ActionBar.Title"
tools:ignore="UnusedAttribute">
<ImageView
android:id="@+id/toolbar_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/logo_action_bar"
android:visibility="gone"
tools:ignore="ContentDescription" />
<Spinner
android:id="@+id/spinner_nav"
style="@style/Widget.MyApp.Spinner.DropDown.ActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
這是我的theme
,我申請到我的工具欄以上:
<style name="MyApp.ThemeOverlay.AppCompat.ActionBar" parent="Theme.AppCompat">
<item name="android:homeAsUpIndicator">@drawable/ic_arrow_back</item>
<item name="homeAsUpIndicator">@drawable/ic_arrow_back</item>
</style>
當菜單搜索項目展開時,它不適用。它在崩潰後適用。任何想法爲什麼? – Intern
如何在此代碼之外設置ActionBar? –
也許你應該添加類似於onMenuItemActionCollapse() –