Java 将指示器添加到按钮背景

Java 将指示器添加到按钮背景,java,android,xml,button,drawable,Java,Android,Xml,Button,Drawable,我正在尝试向我的按钮添加指示器。 与此类似: 我怎样才能做到?我能够制作一个坚实的背景,但我不知道如何添加蓝色的小块部分。感谢您的帮助 谢谢你可以这样做 通过在按钮上方添加颜色为的视图 <LinearLayout android:layout_width="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical"

我正在尝试向我的按钮添加指示器。 与此类似:

我怎样才能做到?我能够制作一个坚实的背景,但我不知道如何添加蓝色的小块部分。感谢您的帮助

谢谢你可以这样做 通过在按钮上方添加颜色为的视图

 <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>

在单击时设置其可见性

您可以这样做 通过在按钮上方添加颜色为的视图

 <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" />
<item
    android:drawable="@drawable/fav_selected"
    android:state_checked="true" />
<item
    android:drawable="@drawable/fav_normal" />

fav_selector.xml

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


这样,您就可以创建选择器,您可能需要为其创建单选按钮

将此单选按钮添加到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" />
<item
    android:drawable="@drawable/fav_selected"
    android:state_checked="true" />
<item
    android:drawable="@drawable/fav_normal" />

fav_selector.xml

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


这样,您就可以创建选择器,您可能需要为其设置任何单选按钮

除非和直到您没有其他选项,否则将可见性设置为查看不是最佳做法。除非和直到您没有其他选项,否则将可见性设置为查看不是最佳做法。