单选按钮的自定义图标

问题描述 投票:5回答:3

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

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

我的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>

有人可以指导我吗?

android radio-button custom-component
3个回答
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"/>

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

android:button="@drawable/radio"

这里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>

//比在你的布局中使用这个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>

0
投票

试试这个

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:orientation="horizontal"
    android:layout_marginLeft="10dp"
    android:id="@+id/segment_img"

    android:layout_gravity="right|center_vertical"
    android:checkedButton="@+id/button_add">
        <RadioButton
            android:id="@+id/button_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/violetbg"
            android:button="@drawable/malewhite"  />

        <RadioButton
            android:id="@+id/button_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:button="@drawable/female"  />       
</RadioGroup>
© www.soinside.com 2019 - 2024. All rights reserved.