android - RadioGroup 在 Android 中右对齐

标签 android android-layout

我在左侧有一个 textview,在右侧有一个 RadioGroup。在我将按钮放入单选组之前,我的按钮位于屏幕的右侧。将它放入 RadioGroup 后我做错了什么?

    <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.25" >

  <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/radioButton1"
                android:layout_alignParentLeft="true"
                android:text="@string/hair"
                android:textAppearance="?android:attr/textAppearanceMedium" />

             <RadioGroup
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >


            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="@string/Yes" />

            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/radioButton1"
                android:layout_below="@+id/radioButton1"
                android:text="@string/No" />



            </RadioGroup>


        </RelativeLayout>

最佳答案

您在按钮上单独设置的所有“对齐”参数(如 layout_alignParentRight)不再有效,因为按钮位于 RadioGroup 内,它是 LinearLayout 的子类。为了将组作为一个整体右对齐,您需要将适当的参数添加到 RadioGroup 本身。

此外,您可能希望 RadioGroup 宽度为 wrap_content 而不是 fill_parent。否则,当容器试图填满所有可用空间时,您所做的任何水平布局对齐都可能不可见。

HTH

关于android - RadioGroup 在 Android 中右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8451059/

相关文章:

javascript - 内置 Phonegap 的应用程序 - 如何使元素可拖动?

java - 应用程序在 backpress 后恢复时布局元素背景颜色发生变化

java - 仅在OptionsItemSelected 上显示文本的弹出窗口

android - 可滚动的 ListView 和一个 TextView

Java将字节数组转换为UTF-8字符串

android - 执行与清除数据按钮相同的操作

android - 为什么 fragment 中的生命周期范围.launch 会阻塞 UI 线程?

android - 使用嵌套 ScrollView 问题的响应式设计

android - 具有列表和图像按钮的灵活的相对布局

android Edittext提示问题