Android:解决RadioGroup中RadioButton的图片自定义及每项间隔距离一样


这个用于控制每个radiobutton之间的距离一致。
android:layout_weight="1"
以下3个属性是自定义radiobutton的图片
android:background="@null"
android:button="@null"
android:drawableLeft="@drawable/btn_radio_fee"

注意:不能使用android:layout_marginRight、android:paddingLeft
     不管是在布局文件中还是在代码里写,不同的屏幕分辨率会不一致。





/res/drawable-hdpi该目录下有2中图片,一张是选中状态的,一张是位选中状态


创建xml:/res/drawable/btn_radio_fee.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/radio_unchecked"/>
    <item android:state_checked="true"  android:drawable="@drawable/radio_checked"/>
    <item android:state_selected="true"  android:drawable="@drawable/radio_checked"/>
    <item android:state_pressed="true"  android:drawable="@drawable/radio_checked"/>
 
</selector>



创建xml:/res/layout/activity_main.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" >

    <RadioGroup
        android:id="@+id/feeGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dip"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/feerb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawableLeft="@drawable/btn_radio_fee"
            android:gravity="center"
            android:text="20"
            android:textColor="#ff000000"
            android:textSize="12sp" />

        <RadioButton
            android:id="@+id/feerb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawableLeft="@drawable/btn_radio_fee"
            android:gravity="center"
            android:text="30"
            android:textColor="#ff000000"
            android:textSize="12sp" />

        <RadioButton
            android:id="@+id/feerb3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:checked="true"
            android:drawableLeft="@drawable/btn_radio_fee"
            android:gravity="center"
            android:text="50"
            android:textColor="#ff000000"
            android:textSize="12sp" />

        <RadioButton
            android:id="@+id/feerb4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawableLeft="@drawable/btn_radio_fee"
            android:gravity="center"
            android:text="100"
            android:textColor="#ff000000"
            android:textSize="12sp" />

        <RadioButton
            android:id="@+id/feerb5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawableLeft="@drawable/btn_radio_fee"
            android:gravity="center"
            android:text="200"
            android:textColor="#ff000000"
            android:textSize="12sp" />
    </RadioGroup>

</RelativeLayout>



package com.example.radiobtn2;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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

}


猜你喜欢

转载自dengli19881102.iteye.com/blog/2094904