2016-01-26 10 views
13

自分自身で答えを見つけようとしましたが見つかりませんでした。ツールバーにバッジを追加する方法MenuItemアイコン

私はこのように、ツールバーでのMenuItemのアイコンにバッジを作る必要があります。

enter image description here

どのように私はこれを行うことができますか?

+1

すでにバッジを作る方法については、ここでたくさんの例があります。どんな問題がありますか? –

答えて

2

私はそれが可能だと思う:

<ImageView 
     android:id="@+id/counterBackground" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/unread_background" /> <!-- your icon --> 

    <TextView 
     android:id="@+id/count" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="1" 
     android:textSize="8sp" 
     android:layout_centerInParent="true" 
     android:textColor="#FFFFFF" /> 

そしてbackground .alsoとしてそのアイコンのためにそれを使用して、削除/あまりにもデフォルトのアイコンを無効にする必要があります。

あなたは見て利用したい場合があります。また、[android-sdk]/platforms/android-22/data/resにそのアイコンがあるはず

Remove App Icon from Navigation Drawer ActionBar Does not work

Remove the navigation drawer icon and use the app icon to open

https://stackoverflow.com/a/29160904/4409113

を、あなただけのことと使用を見つける必要がありますあなたの目的のために(例えば、その画像ビューを追加し、それをbackgroundのように追加する)

を見てみましょう:ここhttps://stackoverflow.com/a/34999691/4409113

13

は、ステップ機能によってステップです:

menu.xml

<?xml version="1.0" encoding="utf-8"?> 
    <menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 

     <item 
       android:id="@+id/actionNotifications" 
       android:icon="@drawable/ic_info_outline_white_24dp" 
       android:menuCategory="secondary" 
       android:orderInCategory="1" 
       android:title="Cart" 
       app:actionLayout="@layout/notification_layout" 
       app:showAsAction="always" /> 
    </menu> 

を追加しnotification_layout.xmlを追加し、このレイアウトは、通知として使用されますアイコンのレイアウト

 <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     style="@android:style/Widget.ActionButton" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" 
     android:background="@android:color/transparent" 
     android:clickable="true" 
     android:gravity="center" 
     android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/hotlist_bell" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:contentDescription="Notification Icon" 
     android:gravity="center" 
     android:src="@drawable/ic_info_outline_white_24dp" /> 

    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/txtCount" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="@dimen/x5dp" 
     android:layout_marginLeft="@dimen/x10dp" 
     android:layout_marginRight="0dp" 
     android:background="@drawable/pointer_" 
     android:gravity="center" 
     android:minWidth="17sp" 
     android:text="0" 
     android:textColor="#ffffffff" 
     android:textSize="12sp" /> 
    </RelativeLayout> 

今すぐアクティビティ

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

    final View notificaitons = menu.findItem(R.id.actionNotifications).getActionView(); 

    txtViewCount = (TextView) notificaitons.findViewById(R.id.txtCount); 
    updateHotCount(count++); 
    txtViewCount.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      updateHotCount(count++); 
     } 
    }); 
    notificaitons.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
    // TODO 
     } 
    }); 


    return true; 
} 

あなたはカウンターを更新するアクティビティの内側(stackoverflowのから取られた)次の関数を入れることができます:

public void updateHotCount(final int new_hot_number) { 
    count = new_hot_number; 
    if (count < 0) return; 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      if (count == 0) 
       txtViewCount.setVisibility(View.GONE); 
      else { 
       txtViewCount.setVisibility(View.VISIBLE); 
       txtViewCount.setText(Integer.toString(count)); 
       // supportInvalidateOptionsMenu(); 
      } 
     } 
    }); 
}  
+0

あなたの下位のコードで言うことを意味していました: 'if(count == 0) txtViewCount.setVisibility(View.GONE);' –

+0

はい、カウント== 0、次にView.Goneの場合はYesです。 –

+1

メニュー項目をツールバーの左に揃えるにはどうしたらいいですか? (上記の画像のとおり) – Mucahit

関連する問題