私はこのようなものが欲しい:
3番目のアイコンは通知用で、現在はpng画像です。テキスト/番号、つまり03をプログラムで変更して、実際の通知数を表示できるようにするために、何かを行うことは可能ですか。
ありがとうございました
私はこのようなものが欲しい:
3番目のアイコンは通知用で、現在はpng画像です。テキスト/番号、つまり03をプログラムで変更して、実際の通知数を表示できるようにするために、何かを行うことは可能ですか。
ありがとうございました
これが私のために働いたいくつかのサンプルコードです。
1:バッジメニュー項目のレイアウトを作成します。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right" >
<!-- Menu Item Image -->
<ImageView
android:layout_width="48dp"
android:layout_height="fill_parent"
android:clickable="true"
android:src="@drawable/bkg_actionbar_notify_off" />
<!-- Badge Count -->
<TextView
android:id="@+id/actionbar_notifcation_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="@dimen/padding_small"
android:text="99"
android:textColor="@color/holo_orange_dark" />
</RelativeLayout>
2:res / menuにメニュー項目を作成し、actionLayoutをレイアウトに設定します
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/badge"
android:actionLayout="@layout/actionbar_badge_layout"
android:icon="@drawable/icn_menu_posts"
android:showAsAction="always">
</item>
</menu>
3:次に、アクティビティまたはフラグメントのonCreateOptionsMenuで、次のようなことを実行できます...
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.badge, menu);
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
tv.setText("12");
}
注:後でバッジ数を変更したい場合は、onCreateOptionsMenuに渡されたMenuオブジェクトへの参照を保存し、同じコードを使用して必要なビューを取得し、値を設定できます。
===ApCompat警告============================================= =====
AppCompatActivityを使用する場合は、でactionViewを設定する必要があります。onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.badge);
MenuItemCompat.setActionView(item, R.layout.actionbar_badge_layout);
RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(item);
TextView tv = (TextView) notifCount.findViewById(R.id.actionbar_notifcation_textview);
tv.setText("12");
return super.onCreateOptionsMenu(menu);
1つのオプションは、このための独自のアクションビューを作成することです。それを操作するために、インフレーション後にandroid:actionLayout
XMLおよびJavaで使用します。getActionView()
アクションビューはImageView
(アイコンの場合)そして...バッジの場合になります。テキストを介してそのバッジを作成しようとするのは困難であり、アイコンの上に重ねたバッジ画像の束をより適切に提供できると思います(たとえば、RelativeLayout
またはを介してFrameLayout
、またはラッピングすることによって)のアイコンLayerListDrawable
)。
もう1つのオプションは、LevelListDrawable
実行時に選択するアイコン+バッジのNバージョン(おそらくはでラップされている)を用意することです。
domji84の回答にいくつかの変更を加える必要がありました。何らかの理由で、画像ビューのクリックがクリックイベントを呼び出していなかったため、画像ビューからclicklabe="true"を削除した後に検索が機能します。
menu_item_cart.xml
<!-- Menu Item Image -->
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<!-- Badge Count -->
<TextView
android:id="@+id/cartCountTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="5dp"
android:layout_marginTop="5dp"
android:text="99"
android:textColor="@android:color/white"
android:textStyle="bold"/>
</RelativeLayout>
menu_main.xml
<menu 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"
tools:context=".MainActivity">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item
android:id="@+id/action_cart"
android:title="test"
app:actionLayout="@layout/menu_item_cart_count"
app:showAsAction="always">
</item>
</menu>
アクティビティコード
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
menu.findItem(R.id.action_cart).getActionView().setOnClickListener(this);
return true;
}
ここでは、デフォルトのアクションバーでカスタムアクションバーを使用できます...最初にxmlまたはjavaでレイアウトを準備し、次に表示マトリックスを使用してウィンドウマネージャーを取得します。その後、レイアウトを膨らませます。このような
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
ActionBar ab = getSupportActionBar();
View mactionbar = getLayoutInflater().inflate(R.layout.main2, null);