【问题标题】:How to layout views so that radiogroup could be used?如何布局视图以便可以使用无线电组?
【发布时间】:2011-07-12 22:27:00
【问题描述】:

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

<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 之间放置一些空间,我可以使用它。我设法使用 android:drawablePadding="10dip" 在 drawableLeft 和文本之间留出一些空间(在我的上一个屏幕截图中没有看到)。

【问题讨论】:

    标签: android layout radio-button


    【解决方案1】:

    使用单选组和单选按钮,并在 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>
    

    【讨论】:

    • @DixieFlatline 是的,看起来您可以使用 drawablePadding 或者只是在文本的开头添加空格。有点烦人,你不能指定你希望单选按钮在右边而不是左边。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2017-10-10
    • 1970-01-01
    • 2015-12-12
    • 2013-07-19
    相关资源
    最近更新 更多