【问题标题】:How can I align radio buttons to the right of associated text?如何将单选按钮对齐到关联文本的右侧?
【发布时间】:2018-08-29 19:10:32
【问题描述】:

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

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

【问题讨论】:

标签: android android-layout


【解决方案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>

【讨论】:

  • +1 得到很好的答案..我创建了一个自定义单选按钮,但您的解决方案很棒..它被称为存在感
  • @Pragnani 谢谢.. 我也是从别人那里学来的 :)
  • 如果你这样做,那么当父主题是AppCompat 主题时,它不会被正确着色。
  • @toobsco42 使用AppCompat 你可以使用这个drawable android:drawableRight="@drawable/abc_btn_radio_material"
  • 你可以使用 android:drawableRight="?android:attr/listChoiceIndicatorSingle"
【解决方案2】:

通过使用

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

您更改单选按钮格式。最好是右对齐,

android:layoutDirection="rtl"

在每个单选按钮上,文本将在左侧。

【讨论】:

  • 只需添加 android:layoutDirection="rtl" 即可保持 appCompat 主题
  • @alvinmeimoun 知道如果按钮是填充父项,如何从右侧给单选按钮图标填充?
【解决方案3】:

你可以在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"

【讨论】:

    【解决方案4】:

    简单的方法将它添加到您的 xml 中的单选按钮

    android:layoutDirection="rtl"
    

    【讨论】:

      【解决方案5】:

      如果您打算动态添加元素,您可以尝试创建一个布局,其中包含一个文本视图和一个右侧没有文本的单选按钮。 这些方面的内容:

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

      【讨论】:

        【解决方案6】:

        正如我在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 文件中,然后使用&lt;include&gt; 标签将其包含在您的其他布局中(在Apache 2.0 License 下)。您也可以将其用作列表项,并保持不变。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-02
          • 1970-01-01
          • 2019-07-22
          • 1970-01-01
          • 1970-01-01
          • 2011-12-07
          相关资源
          最近更新 更多