Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 把 RadioButton 的圓點放到文字右側

RadioButton 預設把選取圓點放在文字左邊。若設計稿要求圓點在右側,只要善用 android:button 與 android:drawableRight 兩個屬性即可,不必自訂元件。

版面配置

把 android:button 設為 @null 關掉原本左側的圓點,再用 drawableRight 指定系統的 btn_radio 貼圖,視覺上就變成文字在左、圓點在右。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/radiobutton_textview"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="@android:drawable/title_bar"
        android:gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="18dip"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:paddingLeft="30dip"
            android:text="Android 新手"
            android:textSize="20dip" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1px"
            android:background="?android:attr/listDivider" />

        <RadioButton
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:paddingLeft="30dip"
            android:text="Android 高手"
            android:textSize="20dip" />
    </RadioGroup>
</LinearLayout>

中間那個高度 1px 的 View 是分隔線,用系統的 listDivider 當背景,讓兩列選項看起來更像清單。

Java 端監聽選擇變化

package sdfasdf.sadf;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class SdfsadfasdfasdffActivity extends Activity {
    private TextView textView;
    private RadioGroup group;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.radiobutton_textview);
        group = (RadioGroup) findViewById(R.id.group);

        // 單選按鈕組監聽事件
        group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 依 ID 判斷被選取的按鈕
                if (checkedId == R.id.button1) {
                    textView.setText("Android 新手");
                } else {
                    textView.setText("Android 高手");
                }
            }
        });
    }
}

小結

這個技巧同樣適用於 CheckBox:只要切掉內建 button,改用任意 drawable 定位,就能把狀態圖示擺到左、右或上方。若需要更精細的排列,也可以改用 drawablePadding 調整圖示與文字間距。

Copyright © Linux教程網 All Rights Reserved