Android:如何更改单选按钮的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3014597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:19:27  来源:igfitidea点击:

Android: How to change the Size of the RadioButton

androidsizeradio-button

提问by TianDong

I have many RadioButtons in my app. The RadioButtons are too big for me. Is there any way to make them smaller?

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

采纳答案by arclight

Can't be done, the radio button is a built-in control component and as such its size is fixed.

无法完成,单选按钮是内置控件组件,因此其大小是固定的。

回答by

One quick hacky solution is to scale the button down:

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

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

This works great for going smaller.

这非常适合变小。

For going larger, this tends to cause some clipping from the container View, so you'll likely have to hardcode the height/width of the RadioGroup to fit the scaled buttons. The button drawable can also get noticeably pixelated the larger you go, so it's not really great if you want something 3x larger...

为了变大,这往往会导致容器视图中的一些剪辑,因此您可能必须对 RadioGroup 的高度/宽度进行硬编码以适合缩放的按钮。按钮 drawable 也可以在你放大的时候明显像素化,所以如果你想要 3 倍大的东西并不是很好......

回答by IM.

It canbe done but is not as simple as setting the Layout_Width and Layout_height as with EditTexts, Buttons etc. To modify the size/looks of a view like a checkbox/radio button use the "Background" and "Button" properties to specify your own drawables.

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

This is an older page, and the locations are different now, but this'll give you an idea : http://www.anddev.org/tutorial_change_look_of_checkbox-t4553.html

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

回答by Colone1

you can use scalex and scaley properties , then use translationX and translationY to put it in the radiobutton windows.

您可以使用 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" />

回答by Andrey Aleev

I relpaced RadioButtonwith ToggleButtonand applied same style. I did override background and button.

我重新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"/>

and style:

和风格:

<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>

Works for me - looks the same, but with custom height.

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

If your RadioButton is in the RadioGroup, you will need to customize listener, check Android: How to get a radiogroup with togglebuttons?

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

回答by Ric17101

I've done this by adjusting the TextSize of RadioButton itself

我通过调整 RadioButton 本身的 TextSize 来做到这一点

Like so

像这样

android:textSize="20sp"

then apply to my code;

然后适用于我的代码;

<RadioGroup
    android:id="@+id/checkboxRadioButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/rb1"
        style="@android:style/Widget.CompoundButton.CheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/checkBoxMargin"
        android:text="YES"
        android:textSize="20sp" />
    ...

Hope this help to someone

希望这对某人有帮助

回答by Kanaiya Katarmal

<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>