Android: Adding badge to corner of drawable on a button

拈花ヽ惹草 提交于 2019-12-11 09:58:31

问题


What is the best way to add a badge icon (Circle with a number in it) to a button's drawable?

The drawable is just an image. I was hoping there would be a way to change what I am using as the buttons drawable, and perhaps set it to another drawable that has my image in the middle with a shape on the top right. I will need to update the text of the shape programatically.

Here is my button now:

<Button
     android:id="@+id/SpecialsMenuButton"
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"
     android:textColor="#FFFFFF"
     android:text="@string/specials_tab_title"
     android:background="@drawable/menu_button"
     android:padding="4dp"
     android:drawableBottom="@drawable/collections_labels"
     android:textAppearance="?android:attr/textAppearanceSmall"
     android:layout_weight="1" />

I also created a circle badge:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="oval">
  <solid
    android:color="#F00" />
  <stroke
    android:width="2dip"
    android:color="#FFF" />
  <padding
    android:left="5dip"
    android:right="5dip"
    android:top="5dip"
    android:bottom="5dip" />
</shape>

I want to as easily as possible replace the android:drawableBottom with something that will have my image with the shape in the top right corner of the image. Will I be able to do this as a drawable or will I have to do this outside of the button?

Thanks!


回答1:


If you want it as a Drawable you have to create a Bitmap where you draw the circle and the number onto the image of the button.

But I would do it like this: Place the badge as an ImageView and a TextView above your button, so you can easily edit the text of the TextView



来源:https://stackoverflow.com/questions/19234360/android-adding-badge-to-corner-of-drawable-on-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!