Как я могу выровнять переключатели справа от связанного текста?
У меня есть следующие радиокнопки внутри группы радио похожих кнопок. По умолчанию кнопка находится слева от связанного текста. Как я могу заставить кнопку быть справа от связанного с ней текста?
<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>
Ответы
Ответ 1
использовать
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>
Ответ 2
используя
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
вы меняете формат радиокнопки. Лучше всего правильно выравнивать,
android:layoutDirection="rtl"
на каждом radioButton, а текст будет слева.
Ответ 3
Если вы планируете динамически добавлять элементы, вы можете попытаться создать макет с текстовым изображением и радиокнопку без текста справа.
Что-то в этих строках:
<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
простой способ добавить это в свой радиообъект в xml
android:layoutDirection="rtl"
Ответ 5
Вы можете сделать в 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"
Ответ 6
Намного проще, поскольку я разместил здесь, чтобы использовать встроенный макет 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). Вы также можете использовать его как элемент списка и оставить его неизменным.