android - 如何布局 View 以便可以使用 radio 组?

标签 android layout radio-button

现在我有这个布局(垂直线性布局内的水平相对布局),但我需要单选按钮而不是复选框(用户必须只能选择一个选项)。

enter image description here

<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 的解决方案(如下),我得到了这个: enter image description here

如果我可以在单选按钮和它的 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/

相关文章:

Android 单选按钮背景与默认圆圈重叠

android - 如何从 Git 导入 Android Studio 项目

android - 我的 Android 应用程序能否在任何给定时刻知道并检测手机在哪个运营商下运行?

java - 为什么在使用图形时无法查看添加到 JPanel 的 JLabel?

c# - 特定 WInForm 的缩放错误

html - 需要有关 HTML 页面布局的帮助

javascript - CKeditor单选垂直

java - 分数计数器不增加

android - Sherlock ActionBar 溢出菜单 TextColor

javascript - 如何在单选按钮点击功能上添加特定的 id?