项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

示例如下:

/view/selection/RadioButtonDemo2.java

/**
 * RadioGroup - 单选框组
 * RadioButton - 单选框
 *     setButtonDrawable() - 设置单选框图片在各种状态下的样式
 *
 *
 * 本例主要介绍 RadioButton 的样式
 */

package com.webabcd.androiddemo.view.selection;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;

import com.webabcd.androiddemo.R;

public class RadioButtonDemo2 extends AppCompatActivity {

    private RadioButton _radioButton3;
    private RadioButton _radioButton4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_selection_radiobuttondemo2);

        _radioButton3 = (RadioButton)findViewById(R.id.radioButton3);
        _radioButton4 = (RadioButton)findViewById(R.id.radioButton4);

        sample();
    }

    private void sample() {
        _radioButton3.setButtonDrawable(R.drawable.selector_radiobutton_button);
        _radioButton4.setButtonDrawable(R.drawable.selector_radiobutton_button);
    }
}

/layout/activity_view_selection_radiobuttondemo2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--
        RadioGroup - 单选框组(要声明 id 否则会有问题)
        RadioButton - 单选框(要声明 id 否则会有问题)
            button - 设置单选框图片在各种状态下的样式(参见 drawable/selector_radiobutton_button.xml)
    -->

    <RadioGroup
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/selector_radiobutton_button"
            android:checked="true"
            android:text="radioButton1" />

        <RadioButton
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/selector_radiobutton_button"
            android:text="radioButton2" />
    </RadioGroup>


    <!--
        在 java 中设置 RadioButton 的 button
        调整单选框的图标与文字的间距可以通过 padding 来实现
    -->
    <RadioGroup
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <RadioButton
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:text="radioButton3" />

        <RadioButton
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:text="radioButton4" />
    </RadioGroup>

</LinearLayout>

/drawable/selector_radiobutton_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:andro>

    <!--
        state_enabled="true" - 可用
        state_enabled="false" - 不可用
        state_checked="true" - 选中
        state_checked="false" - 未选中

        注意:可以直接在 item 的 drawable 指定图片,但是这样只能按照原图的大小显示,如果需要指定图片的显示大小的话,则需要像本例这样使用 layer-list
    -->

    <item
        android:state_enabled="true"
        android:state_checked="true"
        android:drawable="@drawable/layerlist_radiobutton_button_checked" />
    <item
        android:state_enabled="true"
        android:state_checked="false"
        android:drawable="@drawable/layerlist_radiobutton_button_unchecked" />
</selector>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

相关文章:

  • 2022-02-03
  • 2021-11-20
  • 2022-02-12
  • 2021-09-28
  • 2021-09-23
  • 2021-08-12
  • 2021-08-16
  • 2021-06-24
猜你喜欢
  • 2021-05-21
  • 2021-12-27
  • 2021-07-22
  • 2021-06-19
  • 2021-06-18
  • 2021-10-13
  • 2021-12-27
相关资源
相似解决方案