如何将单选按钮对齐到相关文本的右侧?

时间:2013-04-21 15:54:53

标签: android android-layout

我在类似按钮的广播组中有以下单选按钮。默认情况下,按钮位于关联文本的左侧。如何让按钮本身位于相关文本的右侧?

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

  <RadioButton
    android:id="@+id/do_tastk_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:onClick="doTask1"
    android:paddingLeft="40dip"
    android:text="@string/task_name_1"
    android:textColor="#000000" />

  <RadioButton
    android:id="@+id/do_tastk_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:onClick="doTask2"
    android:paddingLeft="40dip"
    android:text="@string/task_name_2"
    android:textColor="#000000" />

</RadioGroup>

6 个答案:

答案 0 :(得分:52)

使用

    android:button="@null"
    android:drawableRight="@android:drawable/btn_radio"

所以你的代码就像:

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

  <RadioButton
    android:id="@+id/do_tastk_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:button="@null"
    android:drawableRight="@android:drawable/btn_radio"
    android:onClick="doTask1"
    android:paddingLeft="40dip"
    android:text="@string/task_name_1"
    android:textColor="#000000" />

  <RadioButton
    android:id="@+id/do_tastk_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:button="@null"
    android:drawableRight="@android:drawable/btn_radio"
    android:onClick="doTask2"
    android:paddingLeft="40dip"
    android:text="@string/task_name_2"
    android:textColor="#000000" />

</RadioGroup>

答案 1 :(得分:12)

使用

android:button="@null"
android:drawableRight="@android:drawable/btn_radio"

您更改单选按钮格式。最好的做法就是正确对齐,

android:layoutDirection="rtl"

在每个radioButton上,文字将在左侧。

答案 2 :(得分:2)

您可以在xml中执行

<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center" //or "center_vertical" for center text
android:layoutDirection="rtl"
android:text="hello" />

以下行就足够了

android:layoutDirection="rtl"

答案 3 :(得分:1)

如果您计划动态添加元素,可以尝试使用textview和右侧没有文本的单选按钮创建布局。 这些方面的东西:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RadioButton
    android:id="@+id/radiobutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"/>

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/radiobutton"
    android:layout_alignBottom="@+id/radiobutton"
    android:layout_alignParentLeft="true">

答案 4 :(得分:1)

简单的方法是将它添加到xml中的radiobutton

public class PersonDao<T extends Person> { 

     private Class<T> type;

     public PersonDao(Class<T> type) {
         this.type = type;
     }

     public T select(int id) {
        String sql = "SELECT * FROM person WHERE per_id=? and type=?";
        PreparedStatement ps = con.preparedStatement(sql);
        ps.setInt(1, id);
        ps.setString(2, type.getName());
        ResultSet rs = ps.executeQuery();
        T t = null;

        if(rs.next()) {
            t = type.newInstance();

            t.setId(rs.getInt("per_id"));
            t.setName(rs.getString("per_name"));
        }

        return t;
     }

     ...
}

答案 5 :(得分:0)

我发布here的更简单的解决方案是使用内置的Android布局 - android.R.layout.simple_list_item_single_choice

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<CheckedTextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" />

如果要修改它,可以将上述内容复制粘贴到新的XML文件中,然后使用<include>标记将其包含在其他布局中(Apache 2.0 License下)。您也可以将其用作列表项,并保持不变。

相关问题