单选按钮的自定义图标

Tec*_*Frk 6 android radio-button custom-component

我正在尝试为android中的单选按钮实现自定义图标.即我希望单选按钮图标是我自己选择的一个.我可以使用android:button单选按钮内的属性来做到这一点,但我没有在用户单击它时如何定义它.在我的应用程序中,当用户点击它没有任何反应.我也在这里遵循了这个指南.

I have 2 images :
 A. tire.png (When the radio button is not clicked)
 B. tireinvert.png (When the radio button is clicked)
Run Code Online (Sandbox Code Playgroud)

我的RadioButton.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.techfrk.customizeradiobutton.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

     <RadioGroup
                android:id="@+id/newexprgrp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RadioButton
                android:id="@+id/tvllocrbtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="40dp"
                android:text="Travelling Expense (Local)"
                />

                  <RadioButton
                android:id="@+id/tvloutrbtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="15dp"
                android:text="Travelling Expense (Outstation)"

                 />

               <RadioButton
                android:id="@+id/phnexprbtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="15dp"
                android:text="Phone Expense"

                 />

                   <RadioButton
                android:id="@+id/miscexprbtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="15dp"
                android:text="Misc Expense"

                 />
            </RadioGroup>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

有人可以指导我吗?

Rah*_*rma 18

右键单击您的drawable文件夹然后 - > new-> android xml file->选择根元素"selector"然后将代码粘贴到文件中:

<item android:drawable="@drawable/tire" android:state_checked="true"/>
  <item android:drawable="@drawable/tireinvert" android:state_checked="false"/>
Run Code Online (Sandbox Code Playgroud)

然后在您的单选按钮的xml中添加以下行:

android:button="@drawable/radio"
Run Code Online (Sandbox Code Playgroud)

这里radio是我们在drawable文件夹中创建的选择器xml文件


小智 6

//首先在drawable中为自定义单选创建custom_radio_search_location.xml文件

<?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true"
          android:drawable="@drawable/invite_radio_check" />
        <item android:state_checked="false"
          android:drawable="@drawable/invite_radio_uncheck" />
   </selector>
Run Code Online (Sandbox Code Playgroud)

//在布局中使用此custom_radio_search_location.xml

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="3" >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:button="@drawable/custom_radio_search_location"
        android:checked="true"
        android:text="A"
        android:textColor="@android:color/black" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:button="@drawable/custom_radio_search_location"
        android:text="B"
        android:textColor="@android:color/black" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:button="@drawable/custom_radio_search_location"
        android:text="C"
        android:textColor="@android:color/black" />
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)