How to create this toggle button?

扶醉桌前 提交于 2019-12-24 12:44:32

问题


How can I create a toggle button that looks like this?

Currently I have the below:

        <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1">

        <ToggleButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textOff="@string/toggle_button_boy_add_baby"
            android:textOn="toggle_button_boy_add_baby" />

        <ToggleButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textOff="@string/toggle_button_girl_add_baby"
            android:textOn="@string/toggle_button_girl_add_baby" />

        <ToggleButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textOff="@string/toggle_button_unsure_add_baby"
            android:textOn="@string/toggle_button_unsure_add_baby" />

    </RadioGroup>

Any tips or advice would be much appreciated. Thank you.


回答1:


try this,

build.gradle

dependencies {
compile 'info.hoang8f:android-segmented:1.0.6'

}

@style/RadioButton

    <attr name="sc_corner_radius" format="dimension" />
<attr name="sc_border_width" format="dimension" />
<attr name="sc_tint_color" format="color" />
<attr name="sc_checked_text_color" format="color" />

code

     <info.hoang8f.android.segmented.SegmentedGroup
        xmlns:segmentedgroup="http://schemas.android.com/apk/res-auto"
        android:id="@+id/segmented2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="horizontal"
        segmentedgroup:sc_border_width="2dp"
        segmentedgroup:sc_corner_radius="10dp">

        <RadioButton
            android:id="@+id/button21"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="One"
            style="@style/RadioButton" />

        <RadioButton
            android:id="@+id/button22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Two"
            style="@style/RadioButton" />

        <RadioButton
            android:id="@+id/button24"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Two"
            style="@style/RadioButton" />
    </info.hoang8f.android.segmented.SegmentedGroup>


来源:https://stackoverflow.com/questions/34020155/how-to-create-this-toggle-button

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