2016-12-30 3 views
3

Я создавал значок меню корзины с значком и списком слоев, но я обнаружил, что на API 23 значок меню получает нежелательный фон.Значок пункта меню, получивший перекрытие в API 23

enter image description here

activity_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/action_cart1" 
     android:icon="@drawable/ic_menu_cart" 
     android:title="Cart" 
     app:showAsAction="always" /> 
</menu> 

ic_menu_cart.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:drawable="@drawable/cart" 
     android:gravity="center" /> 

    <!-- set a place holder Drawable so android:drawable isn't null --> 
    <item 
     android:id="@+id/ic_badge" 
     android:drawable="@drawable/cart" /> 
</layer-list> 

ниже код для обновления счетчика корзины.

public static void setBadgeCount(Context context, LayerDrawable icon, String count) { 

     BadgeDrawable badge; 

     // Reuse drawable if possible 
     Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge); 
     if (reuse != null && reuse instanceof BadgeDrawable) { 
      badge = (BadgeDrawable) reuse; 
     } else { 
      badge = new BadgeDrawable(context); 
     } 

     badge.setCount(count); 
     icon.mutate(); 
     icon.setDrawableByLayerId(R.id.ic_badge, badge); 
    } 

Я создал xml для разных API, но не работал. Я хочу удалить фон. Прошу помощи!

ответ

1

Как я понимаю, вы должны добавить количество значков и значок. Поэтому добавьте ниже, как это работает.

app:actionLayout="@layout/notification_layout" 

в <item> и создать notification_layout.xml макет и установить, что вы хотите.

так что ваш activity_main.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/action_cart1" 
     android:orderInCategory="100" 
     android:icon="@drawable/ic_menu_cart" 
     android:title="Cart" 
     app:showAsAction="always" 
     android:title="ABD" 
     app:actionLayout="@layout/notification_layout" /> 
</menu> 

notification_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="40.0dip" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:clickable="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false"> 
<!-- Menu Item Image --> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:clickable="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:scaleType="center" 
     android:src="@drawable/ic_navigation_cart_white" /> 
<!-- Badge Count --> 
    <TextView 
     android:id="@+id/actionbar_notifcation_textview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_marginTop="12.0dip" 
     android:layout_marginRight="5.0dip" 
     android:gravity="center" 
     android:minHeight="15.0dip" 
     android:minWidth="15.0dip" 
     android:paddingLeft="3.0dip" 
     android:paddingRight="3.0dip" 
     android:textSize="10.0sp" 
     android:background="@drawable/TextView_design" 
     android:textColor="@android:color/white" /> 
</RelativeLayout> 

выход так:

enter image description here

+0

Не могли бы вы помочь мне с notification_layout.xml кодом. –

+0

Мне нужно точно то, что вы показали на картинке –

+0

@KuldeepDubey см. Я добавляю свой файл «notificaton.layout.xml», теперь меняю изображение и вижу результат. – Ironman

Смежные вопросы