以下を実行することでこれを解決しました。
角丸の形状を単色で作るために作成しました。これにより、半透明の黒も追加され、黒地に対して押された外観になります。
res/drawable/shape_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="#33000000" android:width="2dp"/>
<corners android:radius="4dp" />
<solid android:color="#99333333"/>
</shape>
レイヤー ドローアブルは、アクション バー アイテムの実際のドローアブルとして使用されます。背景 (上記) にレンチ アイコンが重ねられています。
res/drawable/layer_customizer.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/shape_notification" />
<item android:drawable="@drawable/ic_menu_preferences" />
</layer-list>
色を変更する Java コード。ターゲット ビューは、layer_customizer ドローアブルが割り当てられたオブジェクトです。渡された色は、shape_notification.xml のソリッド タグの色を変更します。
public static void setCustomizerDrawableColor(final View target, final int color) {
final Drawable d = target.getDrawable();
LayerDrawable layer = (LayerDrawable)d;
GradientDrawable gradient = (GradientDrawable)layer.getDrawable(0);
gradient.setColor(color);
gradient.invalidateSelf();
layer.invalidateSelf();
target.invalidate();
}
これらのレイヤーを使用してレイアウトを作成します。
res/layout/actionview_customizer.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ActionViewCustomizer"
android:src="@drawable/layer_customizer"
android:contentDescription="@string/customize"
style="@style/ActionBarButton" />
ActionBar に配置するカスタム レイアウトを取得するには、次のメニュー項目を追加します:
res/menu/actionbar_main.xml
<item android:id="@+id/MenuItemCustomize"
android:icon="@drawable/layer_customizer"
android:title="@string/customize"
android:showAsAction="always"
android:actionLayout="@layout/actionview_customizer"
/>
次に、アクション バーをロードした後、このコードを使用してボタンのハンドルを取得します。これはアクティビティで発生します。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar_main, menu);
final ActionBar actionBar = getActionBar();
final MenuItem customizerItem = menu.findItem(R.id.MenuItemCustomize);
View v = customizerItem.getActionView();
customizerActionView = (ImageButton) v;
customizerActionView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onOptionsItemSelected(customizerItem);
}
});
}
完全なソースが一緒に動作するのを見たい場合は、私がこれを使用しているアプリのソース コードを見てください。 /activities/MainActivity.java