【问题标题】:How to align radio button to center如何将单选按钮对齐到中心
【发布时间】:2016-12-28 07:40:51
【问题描述】:

我希望下图中的单选按钮与center 对齐。我使用了重力,但它不起作用。

我用过这段代码

   <RadioGroup
                    android:id="@+id/qualityRadioGroup"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="5"
                    >


                    <RadioButton
                        android:id="@+id/qty1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_gravity="center_vertical"
                        android:button="@drawable/radio_button_selector"
                        android:checked="false"
                        android:tag="1"/>


                    <RadioButton
                        android:id="@+id/qty2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="@dimen/s5dp"
                        android:button="@drawable/radio_button_selector"
                        android:tag="2"
                        />


                    <RadioButton
                        android:id="@+id/qty3"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="@dimen/s5dp"
                        android:button="@drawable/radio_button_selector"
                        android:tag="3"
                        />


                    <RadioButton
                        android:id="@+id/qty4"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="@dimen/s5dp"
                        android:button="@drawable/radio_button_selector"
                        android:tag="4"
                        />


                    <RadioButton
                        android:id="@+id/qty5"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="@dimen/s5dp"
                        android:button="@drawable/radio_button_selector"
                        android:checked="false"
                        android:tag="5"/>


                </RadioGroup>

如果我在线性布局中添加单选按钮而不是对齐到中心,但单选按钮组功能在这种情况下不起作用。

我想要居中对齐的单选按钮以及单选组功能,即一次只能选择一项。

【问题讨论】:

  • 你使用线性还是相对布局
  • 这会有所帮助,看看stackoverflow.com/questions/10464171/…
  • @Don'tBenegative - 它的内部线性布局
  • 尝试相对布局重力等仅适用于相对
  • @Don'tBenegative-Relative 布局也产生相同的结果。

标签: android radio-button layout-gravity


【解决方案1】:

我有解决这个问题的方法

    android:textSize="0.1sp"
    android:drawableTop="@drawable/selector_dashboard_button"
    android:button="@null"

添加了这些东西而不是 android:background=""

这是我的完整布局代码

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">

<RadioButton
    android:id="@+id/radioButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:checked="true"
    android:drawableTop="@drawable/selector_dashboard_button"
    android:padding="10dp"
    android:tag="1"
    android:textSize="0.1sp" />

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:drawableTop="@drawable/selector_inventory_button"
    android:padding="10dp"
    android:textSize="0.1sp" />

<RadioButton
    android:id="@+id/radioButton3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:drawableTop="@drawable/selector_check_list_button"
    android:padding="10dp"
    android:textSize="0.1sp" />

<RadioButton
    android:id="@+id/radioButton4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:drawableTop="@drawable/selector_vendor_button"
    android:padding="10dp"
    android:textSize="0.1sp" />
</RadioGroup>

这是我的代码截图

【讨论】:

    【解决方案2】:

    试试这个我已经测试过 添加 FrameLayout 并为其赋予子权重并从 Radiobutton 中删除权重并将中心布局重力赋予 Radiobutton 它会根据需要提供

        <?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:weightSum="5"
        android:orientation="horizontal">
    
        <RadioGroup
            android:id="@+id/qualityRadioGroup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
    
            android:layout_weight="5"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/qty1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:background="@mipmap/ic_launcher"
                android:button="@null"
    
                android:tag="1" />
    
            <RadioButton
                android:id="@+id/qty2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:padding="5dp"
                android:button="@null"
                android:background="@mipmap/ic_launcher"
                android:tag="2" />
            <RadioButton
                android:id="@+id/qty3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:padding="5dp"
                android:button="@null"
                android:background="@mipmap/ic_launcher"
                android:tag="3" />
    
            <RadioButton
                android:id="@+id/qty4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:padding="5dp"
                android:button="@null"
                android:background="@mipmap/ic_launcher"
                android:tag="4" />
    
            <RadioButton
                android:id="@+id/qty5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:padding="5dp"
                android:button="@null"
                android:background="@mipmap/ic_launcher"
                android:tag="5" />
        </RadioGroup>
    
    </LinearLayout>
    

    对于可绘制选择器,请参考Link

    【讨论】:

    • 在这种情况下 RadioGroup 功能不起作用,即。我们可以选择所有单选按钮
    • 没有得到你能不能给我一个为什么它不工作的原因
    • 它将居中对齐,但它允许多选单选按钮 whareas 我想要单选单选按钮功能
    • @Ragini 单选按钮用于从单选组中仅选择一次。如果要选择多项选择,则必须集成复选框
    • @EthicsDev- 我想要单选。但如果我在框架布局中添加单选按钮,则它允许多选,这是我不想要的
    【解决方案3】:

    老问题,但我刚刚找到了适合我的情况的解决方案:

    android:minWidth="0dp" 添加到您的radioButton,这应该会删除可绘制按钮左右两侧的空格。

    如果您有一个带有 TextView 的 LinearLayout 并希望按钮在 TextView 上方或下方完全水平居中,则效果很好。

    【讨论】:

      【解决方案4】:

      下面提到的代码正在运行。

       <RadioButton
                              android:id="@+id/qty1"
                              android:layout_width="0dp"
                              android:layout_height="wrap_content"
                              android:layout_gravity="center"
                              android:layout_weight="1"
                              android:button="@null"
                              android:checked="false"
                              android:drawableTop="@drawable/radio_button_selector"
                              android:tag="1"/>  
      

      添加
      android:button="@null"
      android:drawableTop="@drawable/radio_button_selector"

      【讨论】:

        【解决方案5】:

        使用边距将圆圈放在中间

        <RadioButton
                            android:id="@+id/qty4"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:gravity="center"
                            android:padding="@dimen/s5dp"
                            android:button="@drawable/radio_button_selector"
                            android:tag="4"
                            android:marginLeft="5dp" 
                            />
        

        对于不同的屏幕,您需要以编程方式进行
        并阅读此SO Thread

        【讨论】:

          【解决方案6】:

          您能否尝试为文本和单选按钮提供相同的布局宽度并赋予它们重力Gravity.CENTER_HORIZONTAL

          谢谢

          【讨论】:

          • 文本和单选按钮都有 android:layout_weight="1"
          【解决方案7】:

          将您的 Linearlayout 替换为 Relativelayout

          因为重力可能不适用于线性布局

          【讨论】:

            【解决方案8】:

            尝试在无线电组中添加重力=“中心”并检查

            <LinearLayout                        
                           android:layout_width="match_parent"
                           android:gravity="center"
                           android:layout_height="match_parent">
                            <RadioGroup
                              android:id="@+id/qualityRadioGroup"
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:orientation="horizontal"                  
                              android:weightSum="5">
                             <RadioButton
                                android:id="@+id/qty1"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:layout_gravity="center_vertical"
                                android:button="@drawable/radio_button_selector"
                                android:checked="false"
                                android:tag="1"/>
                        </LinearLayout>
            

            【讨论】:

            • @Ragini 你正在使用什么布局
            • 它的内部线性布局
            • @Ragini 检查更新的答案尝试在单选组之前添加线性布局并为线性布局添加重力。
            • 在这种情况下 RadioGroup 功能不起作用,即。我们可以选择所有单选按钮
            • @Ragini 然后尝试在 xml 中将单选按钮集选中为 false
            【解决方案9】:

            使用边距和删除权重将圆圈放在中间

             <RadioButton
                    android:id="@+id/qty1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"
                    android:tag="1"/>
            

            【讨论】:

              【解决方案10】:
              • 从 RadioButtons 中删除所有填充
              • 在每个 RadioButton 的任一侧添加一个 LinearLayout
              • 将 LinearLayout 的尺寸设置为所需的边距大小
              • 为这些线性布局设置 onClickListeners

              在您的布局中:

                           <LinearLayout
                              android:id="@+id/btn2_padding_left"
                              android:layout_width="50dp"
                              android:layout_height="match_parent"
                              android:orientation="horizontal"/>
              
                          <RadioButton
                              android:id="@+id/radio_btn_pg2"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:duplicateParentState="true"
                              android:paddingBottom="12dp"
                              android:paddingTop="12dp"
                              android:scaleX="0.5"
                              android:scaleY="0.5" />
                          <LinearLayout
                              android:id="@+id/btn2_padding_right"
                              android:layout_width="50dp"
                              android:layout_height="match_parent"
                              android:orientation="horizontal"/>
              

              在您的活动课程中:

                  btn2_padding_left.setOnClickListener {
                      radio_btn_pg2.isChecked = true
                  }
                  btn2_padding_right.setOnClickListener {
                      radio_btn_pg2.isChecked = true
                  }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2015-08-24
                • 2019-03-09
                • 1970-01-01
                • 1970-01-01
                • 2015-12-27
                • 2021-10-24
                • 2017-01-10
                相关资源
                最近更新 更多