【问题标题】:Android: How to change the Size of the RadioButtonAndroid:如何更改 RadioButton 的大小
【发布时间】:2011-03-02 04:07:33
【问题描述】:

我的应用中有很多RadioButtons。 RadioButtons 对我来说太大了。有没有办法让它们变小?

【问题讨论】:

  • 我想您可以编写自己的控件,但请记住,控件太小可能会使某些用户在触摸模式下更难准确地点击控件。这会降低您应用的可用性。
  • 我找到了这个解决方案,在这里查看我的答案https://stackoverflow.com/a/44310577/6112256

标签: android size radio-button


【解决方案1】:

有另一种方法可以更改大小。 从矢量资产中选中和取消选中导入单选按钮。

创建三个可绘制对象

radio_button_check

  • 指定任意宽度和高度

radio_button_uncheck

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:width="60dp"
            android:height="60dp"
            android:drawable="@drawable/ic_radio_button_unchecked" />
    </layer-list>

custom_radio_button

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/radio_button_check" android:state_checked="true"></item>
    <item android:drawable="@drawable/radio_button_uncheck"></item>
</selector>

单选按钮

在单选按钮中

<RadioButton                                       
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"                                       
    android:button="@drawable/custom_radio_button"
    android:paddingLeft="10dp"
    android:textSize="39dp"
    android:text="1" />

如果要更改文本大小,请使用“android:textSize”属性

在图片中,第一个是自定义单选按钮,第二个是默认单选按钮

【讨论】:

    【解决方案2】:

    目前,Android Vector Asset Studio 有未选中、选中单选按钮的图标。 您可以导入此向量,然后通过在向量 xml 中更改 android:tint 来更改颜色

    然后设置backgroundRadioButtonlike

    bg_radio_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false" android:drawable="@drawable/ic_baseline_radio_button_unchecked_24" />
        <item android:state_checked="true" android:drawable="@drawable/ic_baseline_radio_button_checked_24" />
    </selector>
    

    ..xml

    <androidx.appcompat.widget.AppCompatRadioButton
            android:layout_width="@dimen/dp_24"
            android:layout_height="@dimen/dp_24"
            android:background="@drawable/bg_radio_selector"
            android:button="@null" />
    

    现在您可以将RadioButton 的大小更改为您想要的任何大小,但可能很难点击,因为点击区域非常小。
    为了增加点击区域,我们可以使用这个函数(注意,填充不起作用,因为我们使用RadioButtonbackground属性)(来自How to increase hit area of Android button without scaling background?的解决方案)

    fun View.increaseTouchArea(pixel: Int) {
        val parent = parent as View
        parent.post {
            val rect = Rect()
            getHitRect(rect)
            rect.top -= pixel
            rect.left -= pixel
            rect.bottom += pixel
            rect.right += pixel
            parent.touchDelegate = TouchDelegate(rect, this)
        }
    }
    

    yourRadioButton.increaseTouchArea(context.resources.getDimensionPixelOffset(R.dimen.dp_12))
    

    【讨论】:

      【解决方案3】:

      我将RadioButton 替换为ToggleButton 并应用了相同的样式。我确实覆盖了背景和按钮。

          <ToggleButton
              android:id="@+id/btnToggle1"
              android:layout_width="match_parent"
              android:layout_height="30dp"
              android:layout_weight="1"
              android:checked="true"
              style="@style/ToggleButtonStyle"
              android:button="@null"
              android:textOn="@string/btnTitle"
              android:textOff="@string/btnTitle"/>
      

      和风格:

      <style name="ToggleButtonStyle">
          <item name="android:background">@drawable/background_radiobutton</item>
          <item name="android:textColor">@color/selector_text_radiobutton</item>
          <item name="android:textAppearance">@null</item>
      </style>
      

      对我有用 - 看起来一样,但具有自定义高度。

      如果您的 RadioButton 在 RadioGroup 中,您将需要自定义监听器,检查 Android: How to get a radiogroup with togglebuttons?

      【讨论】:

        【解决方案4】:

        您可以使用 scalex 和 scaley 属性,然后使用 translationX 和 translationY 将其放在单选按钮窗口中。

        <RadioButton
                    android:id="@+id/rbtnfkilo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:scaleX="1.4"
                    android:scaleY="1.4"
                    android:text="Kilogram"
                    android:textColor="#fff"
                    android:textSize="18sp"
                    android:translationX="24dp" />
        

        【讨论】:

          【解决方案5】:

          一个快速的hacky解决方案是缩小按钮:

          <RadioButton
            android:scaleX="0.5"
            android:scaleY="0.5" />
          

          这非常适合变小。

          为了变得更大,这往往会导致容器视图出现一些剪裁,因此您可能必须对 RadioGroup 的高度/宽度进行硬编码以适应缩放按钮。可绘制的按钮也可能会随着您的移动而显着像素化,因此如果您想要 3 倍大的东西,这并不是很好......

          【讨论】:

          • 并添加 android:textSize="[size]sp" 也会缩放单选按钮标签,但匹配按钮和标签大小需要更多硬代码...
          • 缩小按钮大小,但在它们之间留出更大的空间
          【解决方案6】:

          不能,单选按钮是一个内置的控件组件,因此它的大小是固定的。

          【讨论】:

          【解决方案7】:
          <RadioGroup android:layout_width="fill_parent"               
                      android:layout_height="50dp"           
                      android:orientation="horizontal"         
                      android:checkedButton="@+id/first">  
          
           <RadioButton android:id="@+id/first"        
                android:width="50dp"        
                android:height="50dp"        
                android:button="@drawable/button_radio"/> 
          
             <RadioButton android:id="@+id/second"        
                android:width="50dp"     
                android:height="50dp"     
                android:button="@drawable/button_radio"/>
          
             <RadioButton android:id="@+id/third"
                android:width="50dp"
                android:height="50dp"
                android:button="@drawable/button_radio"/>
          
             <RadioButton android:id="@+id/fourth"                                          
                android:width="50dp"              
                android:height="50dp"           
                android:button="@drawable/button_radio"/>           
          </RadioGroup>
          

          【讨论】:

          • ths 不能缩放内部单选按钮样式
          【解决方案8】:

          可以完成,但不像设置 Layout_Width 和 Layout_height 那样简单,如 EditTexts、Buttons 等。要修改复选框/单选按钮等视图的大小/外观,请使用“ Background”和“Button”属性来指定你自己的drawables。

          这是一个较旧的页面,现在位置不同了,但这会给你一个想法: http://www.anddev.org/tutorial_change_look_of_checkbox-t4553.html

          【讨论】:

          • 这是一个损坏的链接。
          猜你喜欢
          • 2018-07-05
          • 1970-01-01
          • 2011-01-10
          • 2019-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-02
          相关资源
          最近更新 更多