现在我有这个布局(垂直线性布局内的水平相对布局),但我需要单选按钮而不是复选框(用户必须只能选择一个选项)。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:orientation="horizontal">
<ImageView
android:src="@drawable/flag_en"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_alignParentLeft="true" />
<TextView
android:text="English"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="10dip"
android:textStyle="bold"
android:textSize="20dip"
/>
<CheckBox android:id="@+id/checkbox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:orientation="horizontal">
<ImageView
android:src="@drawable/flag_dk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_alignParentLeft="true" />
<TextView
android:text="Dansk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textStyle="bold"
android:textSize="20dip"
/>
<CheckBox android:id="@+id/checkbox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<Button
android:id="@+id/ang_btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="ang"
/>
</LinearLayout>
问题是 RadioGroup 必须放置在线性布局内(不是相对的),所以我不能使用上面的布局。
我怎样才能实现相同的布局(多行、3 列、第三列中的单选按钮)并且还使用带单选按钮的单选组?
编辑: 我尝试了 Robby Pond 的解决方案(如下),我得到了这个:
如果我可以在单选按钮和它的 drawableLeft 之间放置一些空间,我就可以使用它。我设法在 drawableLeft 和带有 android:drawablePadding="10dip"的文本之间留出一些空间(在我的最后一个屏幕截图中没有看到)。
最佳答案
使用单选组和单选按钮,并在 RadioButton 上为图像使用 android:drawableLeft。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup ...>
<RadioButton ... android:drawableLeft="@drawable/flag_en" android:text="English" />
</RadioGroup>
</LinearLayout>
关于android - 如何布局 View 以便可以使用 radio 组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5263767/