2013-04-21 131 views
23

我有一个类似按钮的收音机组内的下列单选按钮。默认情况下,按钮位于关联文本的左侧。如何让按钮本身位于相关文本的右侧?如何将单选按钮对齐相关文本的右侧?

<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> 
+0

我用在所有使用默认UI的SDK中实现这一点的诀窍并默认选择行为,在下面的答案:http://stackoverflow.com/a/42734740/4832356 – SiSa 2017-03-11 12:26:24

回答

49

使用

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> 
+0

哦,哇。谢谢。出于某种原因,我认为我需要一个名为“btn_radio”的可绘制对象。但无论如何我都添加了这些线条,并且它很有效! +1。 – learner 2013-04-21 16:27:15

+0

btn_radio是安卓自己的单选按钮资源 – stinepike 2013-04-21 16:27:56

+1

+1对于很好的答案..我创建了一个自定义单选按钮,但是你的解决方案太棒了..它被称为心灵的存在 – Pragnani 2013-04-21 17:00:49

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"> 

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)。您也可以将它用作列表项目,并保持不变。

10

使用

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

更改单选按钮格式。在每个radioButton上右对齐

android:layoutDirection="rtl" 

并且文本将位于左侧。

+0

只需添加'android:layoutDirection =“rtl”'做窍门保持appCompat主题 – alvinmeimoun 2016-04-18 08:57:52

+1

@alvinmeimoun任何想法如何如果按钮与填充父项,则从右侧给出单选按钮图标填充? – 2016-04-27 11:37:07

+1

这应该是被接受的答案。 – vangoz 2017-01-30 06:15:16

1

简单的方法在XML添加到您的单选按钮

android:layoutDirection="rtl" 
1

也可以用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"