首页 > 解决方案 > 使单选按钮图标居中

问题描述

我有 3 个 RadioButton,如下所示在此处输入图像描述

这 。文本居中,而不是图标。我需要图标和文本居中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="match_parent">

<RadioGroup
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content" >

    <RadioButton
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Test1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <RadioButton
        android:layout_gravity="center"
        android:layout_weight="1"
        android:text="Test2"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RadioButton
        android:layout_gravity="center"
        android:layout_weight="1"
        android:text="Test3"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RadioGroup>

标签: androidradio-button

解决方案


你可以试试下面的代码

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"        
        android:gravity="center"
        android:orientation="horizontal">

        <RadioButton
            android:text="Test1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal">

        <RadioButton
            android:text="Test2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal">

        <RadioButton
            android:text="Test3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</RadioGroup>

推荐阅读