【问题标题】:Can't add margins to RadioButtons无法向 RadioButtons 添加边距
【发布时间】:2014-10-07 16:02:52
【问题描述】:

我的布局有问题。我有一个对话框,我想在其中放置 RadioButtons,但正如您在屏幕截图中看到的那样,它们彼此非常接近。如何扩大它们之间的空间?我已经尝试过 Margin Left/Right、Padding Left/Right 等,但这些都不起作用。

截图

我的 .xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/backrepeat_reversed" >
    <TextView android:id="@+id/ml_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/size"
        android:gravity="center"/>
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioGroup  android:id="@+id/radio_group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <RadioButton android:id="@+id/rws"
            android:layout_width="wrap_content"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:button="@null"
            android:text="@string/sw"
            android:gravity="center_horizontal|bottom"
            android:layout_height="match_parent"
            android:textColor="#000000"
            android:textSize="50sp"/>
        <RadioButton android:id="@+id/rwm"
            android:layout_width="wrap_content"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:button="@null"
            android:text="@string/mw"
            android:gravity="center_horizontal|bottom"
            android:layout_height="match_parent"
            android:textColor="#000000"
            android:textSize="50sp"/>
        <RadioButton android:id="@+id/rwb"
            android:layout_width="wrap_content"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:button="@null"
            android:text="@string/bw"
            android:gravity="center_horizontal|bottom"
            android:layout_height="match_parent"
            android:textColor="#000000"
            android:textSize="50sp"/>
    </RadioGroup>
    </LinearLayout>
    <TextView android:id="@+id/percent_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/percentage"
        android:gravity="center"/>
    <NumberPicker 
        android:id="@+id/percent_picker"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />

</LinearLayout>

【问题讨论】:

  • 尝试填充而不是边距。

标签: android android-layout android-xml android-radiogroup


【解决方案1】:

单选按钮并排放置在LinearLayout 中。他们将layout_width 设置为wrap_content。父级(RadioGroup)也设置为wrap_content,其两个父级(LinearLayout,及其父级,即根LinearLayout 元素)也设置为wrap_content。重力也被设置为居中,所以所有东西都被吸引到屏幕的中心,并且仍然挤在一起。

试试:

  • 将根LinearLayout 元素的layout_width 设置为fill_parent
  • 将嵌套的LinearLayoutRadioGroup 的父级)设置为RelativeLayout(即更改元素类型)
  • 为每个RadioButton设置宽度为33%通过添加这个属性/值对:android:layout_weight=".33"

【讨论】:

    【解决方案2】:

    使用 LinearLayout 或其他 Layout 来优化布局。你可以这样做:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/backrepeat_reversed" >
        <TextView android:id="@+id/ml_label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/size"
            android:gravity="center"/>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <RadioGroup  android:id="@+id/radio_group"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center">
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center">
                    <RadioButton android:id="@+id/rws"
                        android:layout_width="wrap_content"
                        android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
                        android:button="@null"
                        android:text="@string/sw"
                        android:gravity="center_horizontal|bottom"
                        android:layout_height="match_parent"
                        android:textColor="#000000"
                        android:textSize="50sp"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center">
                    <RadioButton android:id="@+id/rwm"
                        android:layout_width="wrap_content"
                        android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
                        android:button="@null"
                        android:text="@string/mw"
                        android:gravity="center_horizontal|bottom"
                        android:layout_height="match_parent"
                        android:textColor="#000000"
                        android:textSize="50sp"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center">
                    <RadioButton android:id="@+id/rwb"
                        android:layout_width="wrap_content"
                        android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
                        android:button="@null"
                        android:text="@string/bw"
                        android:gravity="center_horizontal|bottom"
                        android:layout_height="match_parent"
                        android:textColor="#000000"
                        android:textSize="50sp"/>
                </LinearLayout>
            </RadioGroup>
        </LinearLayout>
        <TextView android:id="@+id/percent_label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/percentage"
            android:gravity="center"/>
        <NumberPicker
            android:id="@+id/percent_picker"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            />
    
    </LinearLayout>
    

    试试吧,我觉得有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多