【问题标题】:RadioButton 'android:button' does nothingRadioButton 'android:button' 什么都不做
【发布时间】:2017-10-12 23:08:13
【问题描述】:

我尝试更改 RadioButton 上选中按钮的可绘制对象,但没有显示任何内容;设置drawable只会删除已经存在的默认值。

我使用堆栈溢出来尝试找到解决方案,但是当我尝试实现它时,没有显示可绘制对象。我的选择器似乎很好,因为当我设置显示我的圈子的单选按钮的 background 时。

我确信这是一个非常明显的问题,我会因为没有看到而扇耳光;谁能找出问题所在?

我的单选按钮

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Left"
            android:checked="false"
            android:button="@drawable/radio_button" />
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/radio_button"
            android:text="Right"
            android:checked="true" />
    </RadioGroup>

使用我的选择器

没有我的选择器

我的选择器 XML

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/radio_button_checked"
        android:state_checked="true" />
    <item
        android:drawable="@drawable/radio_button_unchecked" />
</selector>

radio_button_checked.xml

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/primary_button_color"/>
    <stroke
        android:width="2px"
        android:color="#000000"/>
</shape>

radio_button_unchecked.xml

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="2px"
        android:color="#000000"/>
</shape>

【问题讨论】:

    标签: android radio-button android-drawable


    【解决方案1】:

    我认为您看不到任何东西,因为您的可绘制对象没有任何大小。

    尝试在你的xml的形状下定义&lt;size android:width="60dp" android:height="60dp"/&gt;

    【讨论】:

      【解决方案2】:

      你的问题

      如您所见,RadioButton 扩展了 CompoundButton。当你不使用你的选择器时,有一个默认的drawable。让我们看看它的构造函数。

         public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
          super(context, attrs, defStyleAttr, defStyleRes);
      
          final TypedArray a = context.obtainStyledAttributes(
                  attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);
      
          final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
          if (d != null) {
              setButtonDrawable(d);
          }
      

      默认可绘制对象是R.styleable.CompoundButton_button,我的设备上的intrinsicWidth = 96pxintrinsicHeight = 96px

      当你使用你的选择器时,drawableintrinsicWidth = -1pxintrinsicHeight = -1px 所以你看不到它。

      解决方案

      你应该定义你的drawable的大小。

       <?xml version="1.0" encoding="UTF-8" ?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="oval">
      <size android:width="24dp" android:height="24dp"/>
      <stroke
          android:width="2px"
          android:color="#000000"/>
      </shape>
      

      【讨论】:

        猜你喜欢
        • 2019-03-08
        • 2016-12-24
        • 2017-04-18
        • 2011-08-03
        • 2014-05-11
        • 2014-09-10
        • 1970-01-01
        • 2023-03-29
        • 2013-09-28
        相关资源
        最近更新 更多