【问题标题】:RadioButton: align 3 buttons with background horizontallyRadioButton:将 3 个按钮与背景水平对齐
【发布时间】:2016-04-11 20:02:32
【问题描述】:

我现在遇到了问题。
我有一种情况,我需要显示 3 个按钮,当时只能激活一个按钮。
我曾经使用“ImageButton”,自己处理点击/未点击状态。 但是,我选择将其切换为 RadioGroup,因为它应该处理这种哦行为。
我想这样做:

  • 3 个单选按钮必须位于同一行。
  • 无论使用何种设备,用作背景的图片比例都必须保持不变
  • 如果可能,我希望按钮占据相同百分比的水平空间,无论设备如何。**

我试过了,权重属性,alignParentLeft,right,hard-sized,但找不到干净的解决方案。

你能帮帮我吗?

这里是按钮:

<RadioGroup
    android:id="@+id/mobiprint_mod_radio_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton
        android:id="@+id/radio_tag"
        android:layout_width="96dip"
        android:layout_height="68dip"
        android:background="@drawable/tag_on"
        android:button="@android:color/transparent"
        android:layout_margin="0dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <RadioButton
        android:id="@+id/radio_pe"
        android:layout_width="96dip"
        android:layout_height="68dip"
        android:background="@drawable/pe_off"
        android:button="@android:color/transparent"
        android:layout_margin="0dp"
        android:layout_centerHorizontal="true"/>

    <RadioButton
        android:id="@+id/radio_a7"
        android:layout_width="96dip"
        android:layout_height="68dip"
        android:background="@drawable/a7_off"
        android:button="@android:color/transparent"
        android:layout_margin="0dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RadioGroup>

【问题讨论】:

  • 请粘贴带有属性的 RadioGroup 开始标签。

标签: android xml layout android-radiogroup


【解决方案1】:

试着把下面的代码。 您需要将无线电组的方向定义为水平并根据您的方便应用weightsum 以及应用layout_weight

<RadioGroup
    android:id="@+id/mobiprint_mod_radio_group"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal"
    android:weightSum="1">

    <RadioButton
        android:id="@+id/radio_tag"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.33"
        android:text="option A" />

    <RadioButton
        android:id="@+id/radio_pe"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.33"
        android:text="option B" />

    <RadioButton
        android:id="@+id/radio_a7"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.33"
        android:text="option C" />
</RadioGroup>

希望它对你有用:)

【讨论】:

  • 您好,感谢您的帮助,问题是,这样,我的按钮高度将始终为 60dp。但是,由于“重量”值,它们的宽度会改变,每个按钮的背景比例不会保持:/你有什么想法吗?
  • 如果您正在为单个静态屏幕设计应用程序,您可以使用相对布局并根据需要简单地拖放单选按钮。否则为多个屏幕设置重量根据您的方便。如果您可以分享您真正想要设计的图像,那将会很有帮助..
【解决方案2】:

由于RadioGroup 继承自LinearLayout,您可以在RadioButtons 上使用权重
Radiogroup 中,将方向设置为水平,就像使用 LinearLayout 一样。

<RadioButton
    android:id="@+id/radio_tag"
    android:layout_height="68dip"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:background="@drawable/tag_on"
    android:button="@android:color/transparent"
    android:layout_margin="0dp"
/>

<RadioButton
    android:id="@+id/radio_pe"
    android:layout_height="68dip"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:background="@drawable/pe_off"
    android:button="@android:color/transparent"
    android:layout_margin="0dp"
/>

<RadioButton
    android:id="@+id/radio_a7"
    android:layout_height="68dip"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:background="@drawable/a7_off"
    android:button="@android:color/transparent"
    android:layout_margin="0dp"
/>

我删除了那些属于RelativeLayout的属性。

注意:权重都是1,宽度都是0dp
这是故意的。

【讨论】:

  • 您好,谢谢!这样,我就有了 3 个按钮,但由于高度是固定的,并且宽度会因重量值而变化,因此不会尊重用作每个按钮背景的图片的比例。你对此有什么想法吗?谢谢!
  • 背景,根据定义,被缩放以匹配容器大小。如果您想要固定图像,请考虑使用button 属性,而不是:android:button="@drawable/a7_off"
  • 试过了...这样,当屏幕不够宽时,我想用作按钮的图片会被裁剪。我本来希望这样,如果屏幕变得太小,我的图片高度会随着它的宽度而减小。
【解决方案3】:
  • 将 Radio Group 宽度设置为 match_parent 或在 dip 中设置您想要的任何宽度
  • 在两个单选按钮之间添加视图(虚拟视图),即在第 1 和第 2 之间以及在第 2 和第 3 之间,使用 android:layout_weight="1"

    <RadioGroup
        android:id="@+id/mobiprint_mod_radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <RadioButton
            android:id="@+id/radio_tag"
            android:layout_width="96dip"
            android:layout_height="68dip"
            android:background="@drawable/tag_on"
            android:button="@android:color/transparent" />
    
        <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    
        <RadioButton
            android:id="@+id/radio_pe"
            android:layout_width="96dip"
            android:layout_height="68dip"
            android:background="@drawable/pe_off"
            android:button="@android:color/transparent" />
    
        <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    
        <RadioButton
            android:id="@+id/radio_a7"
            android:layout_width="96dip"
            android:layout_height="68dip"
            android:background="@drawable/a7_off"
            android:button="@android:color/transparent" />
    </RadioGroup>   
    
  • 空格也可以用来代替视图

       <Space
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-07
    • 2021-06-06
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2020-12-20
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多