将指示器添加到按钮背景

Add indicator to button background

我正在尝试向我的按钮添加一个指示器。 类似这样:Image

我该怎么做?我可以制作纯色背景,但我不知道如何添加小蓝块部分。感谢任何帮助。

谢谢

你可以这样做 通过在按钮上方添加一个带有您的颜色的视图

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        android:layout_height="wrap_content">

        <View
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:visibility="visible"
            android:background="#000000" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

设置其在点击时的可见性

在您的 xml 文件中添加此单选按钮

<RadioButton
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/home_selector"
android:button="@android:color/transparent"
android:checked="true" />

<RadioButton
    android:id="@+id/favourite"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/fav_selector"
    android:button="@android:color/transparent"
    android:checked="true" />
....

现在您可以根据需要为每个不同的单选按钮创建一个选择器

home_selector.xml

<item
    android:drawable="@drawable/home_selected"
    android:state_checked="true" />
<item
    android:drawable="@drawable/home_normal" />

fav_selector.xml

<item
    android:drawable="@drawable/fav_selected"
    android:state_checked="true" />
<item
    android:drawable="@drawable/fav_normal" />

像这样,您可以为您可能想要的任何单选按钮创建选择器