2017-08-29 62 views
-1

我需要「在動作條購物車圖標就像附加的圖像 與圖標哪個用戶添加到購物車的項目的數量文本」在Android的菜單項

Image show icon with text on it

+2

你有問題嗎? –

+0

你有沒有嘗試或嘗試任何東西? –

+0

有人給我這個鏈接 https://stackoverflow.com/questions/35009812/how-to-add-badges-on-toolbar-menuitem-icons?fref=gc 它解決所有 –

回答

0

考慮到功能,我相信你需要實現一些類似的佈局條件:

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/mainToolbar" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="?attr/colorPrimary"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="?android:actionBarSize" 
    android:layout_marginRight="10dp" 
    android:gravity="right|center_vertical" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/main_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     app:srcCompat="@drawable/heart_icon" /> 

    <ImageView 
     android:layout_width="10dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="-20dp" 
     app:srcCompat="@drawable/number_icon" /> 
</LinearLayout> 

0

okey然後你需要手動創建你的圖標菜單欄以對其進行完全控制。

首先在activity.xml

<android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize"     
      app:popupTheme="@style/AppTheme.PopupOverlay" > 
      <!-- set any fixed size for 'width' but DO NOT set wrap_content--> 
      <RelativeLayout 
       android:layout_width="48dp" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:id="@+id/icon_image_view" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="[YOUR ICON]"/> 
       <!-- you can set any attributes you want (background-fixed position .. etc) 
        and you can also remove alignParentBottom , alignParentEnd .. 
        i just add it to be clear --> 
       <TextView 
        android:id="@+id/notification_text_view" 
        android:layout_width="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_alignParentEnd="true" 
        android:layout_height="wrap_content" /> 

      </RelativeLayout> 


     </android.support.v7.widget.Toolbar> 

那麼你可以,如果你使用的片段,然後做這些步驟直接訪問他們,如果你使用的活動..

片段類:兩個IMAGE_ICON和text_notification

private ImageView iconImageView; 
private TextView textNotificationView; 


public void setIconImageView(ImageView iconImageView){ 
    this.iconImageView= iconImageView; 
} 
public void setTextNotificationView(TextView textNotificationView){ 
    this.textNotificationView= textNotificationView; 
} 

,那麼你需要手動設置片段添加二傳手。 在活動課

if(savedInstanceState == null) { // for rotation 
     [FragmentClass] fragment = new [FragmentClass](); 
     fragment.setIconImageView((ImageView)findViewById(R.id.child_list_search_edit_view)) 
     fragment.setTextNotificationView((TextView)findViewById(notification_text_view)) 

     getSupportFragmentManager().beginTransaction() 
       .add(R.id.fragment, fragment, [Fragment TAG]).commit(); 
    }else{ 

     [FragmentClass] childActivityFragment =([FragmentClass]) getSupportFragmentManager() 
       .findFragmentByTag([Fragment TAG]); 

     fragment.setIconImageView((ImageView)findViewById(R.id.child_list_search_edit_view)) 
     fragment.setTextNotificationView((TextView)findViewById(notification_text_view)) 

    } 

現在,只要你想,你可以添加您的通知。

+0

感謝我的問題,我已經嘗試過這個解決方案並且對我很好用 https://stackoverflow.com/questions/35009812/how-to-add-badges-on-toolbar-menuitem-icons?fref=gc –