Googleの「質問する」のようなアクションバー通知カウントアイコン(バッジ)

Googleの「質問する」のようなアクションバー通知カウントアイコン(バッジ)

Google の例のように、カウント付きのアクション バー通知アイコンを表示する Android 標準のバッジまたは方法はありますか?

写真の3を数える

そうでない場合、それを作成する最善の方法は何ですか?
私は Android 初心者なので、助けてください。

ベストアンサー1

これが最善の解決策かどうかはわかりませんが、私にとっては必要なことです。

パフォーマンスや品質を向上させるために何を変更する必要があるかご存知でしたら教えてください。私の場合は、ボタンがあります。

メニューのカスタム項目 - main.xml

<item
    android:id="@+id/badge"
    android:actionLayout="@layout/feed_update_count"
    android:icon="@drawable/shape_notification"
    android:showAsAction="always">
</item>

カスタム シェイプ描画可能 (背景の正方形) - 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="#22000000" android:width="2dp"/>
    <corners android:radius="5dp" />
    <solid android:color="#CC0001"/>
</shape>

私のビューのレイアウト - feed_update_count.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/notif_count"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:minWidth="32dp"
     android:minHeight="32dp"
     android:background="@drawable/shape_notification"
     android:text="0"
     android:textSize="16sp"
     android:textColor="@android:color/white"
     android:gravity="center"
     android:padding="2dp"
     android:singleLine="true">    
</Button>

MainActivity - ビューの設定と更新

static Button notifCount;
static int mNotifCount = 0;    

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.main, menu);

    View count = menu.findItem(R.id.badge).getActionView();
    notifCount = (Button) count.findViewById(R.id.notif_count);
    notifCount.setText(String.valueOf(mNotifCount));
    return super.onCreateOptionsMenu(menu);
}

private void setNotifCount(int count){
    mNotifCount = count;
    invalidateOptionsMenu();
}

おすすめ記事