【问题标题】:RadioButton with vector drawables带有矢量绘图的 RadioButton
【发布时间】:2017-04-23 18:05:59
【问题描述】:

我正在尝试使用 RadioButtons 制作性别切换器。现在看起来像 this.

我使用矢量绘图作为图标。当按下按钮时,我想将其设为@color:colorAccent 并将其背景更改为白色圆圈。怎么做比较好?

layout.xml

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:layout_weight="1"
                android:layout_marginEnd="8dp"
                android:button="@drawable/custom_btn_radio"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton2"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:layout_weight="1"
                android:layout_marginStart="8dp"
                android:button="@drawable/custom_btn_radio"/>

        </LinearLayout>
    </RadioGroup>

custom_btn_radio.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
    android:drawable="@drawable/human_male"/>
<item android:state_checked="true"
    android:drawable="@drawable/human_male"
    android:background="@android:color/white"
    android:tint="@color/colorAccent"/>

【问题讨论】:

    标签: java android xml radio-button vector-graphics


    【解决方案1】:

    您应该使用&lt;selector&gt; 为不同的状态选择不同的drawable。

    然后对于颜色,当您使用矢量可绘制对象时,您可以为其属性设置动画:AnimVector 因此,您可以为颜色设置动画,即使它是零持续时间动画(理论上,这不是什么我已经完成了)。

    【讨论】:

    • 我现在正在使用选择器(问题中提供了 XML),但它不起作用,按钮看起来总是未选中。
    • 你现在必须使用android:button="@drawable/custom_btn_radio"/&gt; 如果你这么称呼它,可能应该在名称中提到male
    • 我已经在使用android:button="@drawable/custom_btn_radio"/&gt;,只是在添加选择器时忘记更新相关代码
    • 如果还是什么也看不到,不如试试checked->male unchecked->female。如果这样可行,那只是你不能使用背景或色调。
    • 奇怪,这里有几样东西可以尝试:stackoverflow.com/questions/12432553/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2016-01-10
    • 2021-07-28
    相关资源
    最近更新 更多