简体   繁体   English

如何减少Android RadioButton上可绘制按钮的大小

[英]How to reduce the size of the button drawable on an Android RadioButton

I have six different images, with both selected, and unselected versions. 我有六个不同的图像,分别具有选定和未选定的版本。 I'd like to turn these images into radio buttons, so I made 100px x 100px versions of them, thinking that will be plenty for the radio button to just scale them down to a proper radio button size. 我想将这些图像变成单选按钮,所以我制作了100px x 100px的版本,以为单选按钮将其缩小到适当的单选按钮大小就足够了。 I set the RadioButton 's android:button attribute to xml drawables with selectors for the checked and unchecked states, and I made the radio group's orientation horizontal. 我将RadioButtonandroid:button属性设置为xml drawables,并带有用于选中和未选中状态的选择器,并且使单选android:button组的方向为水平。

I get a row of large images, with their right sides cut off. 我得到一排大图像,其右侧被切除。 Is there a way to scale the buttons of a RadioButton down to a reasonable size, without having to make more drawables? 有没有一种方法可以将RadioButton的按钮缩小到合理的大小,而不必制作更多可绘制对象?

I've tried making the android:button attribute be @null , and applying my drawable, selector, xmls to android:background but that doesn't keep my row of buttons square. 我尝试将android:button属性设置为@null ,并将可绘制的选择器xmls应用到android:background但这不会使我的按钮行保持方形。 I've also tried forcing the layout_height of the button to be a smaller number, but then it just cuts off the top and bottom of the image, with no scaling. 我还尝试将按钮的layout_height强制设置为较小的数字,但随后它仅切掉了图像的顶部和底部,没有缩放比例。

As a side question, is it better to have a bunch of versions of the buttons for different DPIs, and where can I learn more about how big things need to be for each dpi scale/folder? 作为附带的问题,为不同的DPI设置一堆按钮是否更好,并且在哪里可以了解到每个dpi比例/文件夹需要多大的空间?

Update 更新

I used the original svg files I had of the graphics and converted them into on and off VectorDrawables, which should be able to be easily scaled, right? 我使用了图形的原始svg文件,并将它们转换为VectorDrawables的开和关,应该可以轻松缩放,对吧? I'm at a loss for what to do because no one seems to have this issue, since they just make their drawables the size they need for the radio button, so no scaling needed. 我无所适从,因为似乎没有人遇到这个问题,因为他们只是将可绘制对象的大小设为单选按钮所需的大小,因此不需要缩放。

After banging my head against a wall forever on this, I decided that I'd try making my own custom radio button view. 在此之后,我将头永远撞在墙上,之后,我决定尝试制作自己的自定义单选按钮视图。 And as it turns out, what I needed ended up being super simple! 事实证明,我所需要的最终变得超级简单! First, I made a new class, called something like ImageRadioButton, then extended AppCompatRadioButton. 首先,我创建了一个新类,称为ImageRadioButton,然后扩展了AppCompatRadioButton。 I only needed to override the onMeasure method and read the attempted width of the radio button, and set the height to the aspect ratio of the width. 我只需要重写onMeasure方法并读取单选按钮的尝试宽度,然后将高度设置为宽度的长宽比即可。 Something like this: 像这样:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int desiredWidth = MeasureSpec.getSize(widthMeasureSpec);
    Drawable drawable = getBackground();

    float aspectRatio = (float)drawable.getIntrinsicHeight() / (float)drawable.getIntrinsicWidth();
    setMeasuredDimension(desiredWidth, (int)(desiredWidth * aspectRatio));
}

With the images I'm using set as the background of the radiobutton, the text empty, and button null, this works exactly as I needed, without having to make new drawables of different sizes. 使用我使用的图像作为单选按钮的背景,文本为空,按钮为null时,这完全可以按照我的需要工作,而不必制作不同大小的新可绘制对象。

This new radio button won't work for vertically aligned radio buttons, unless their widths are constrained, and since the background is taken up by the graphic, it can't have a separate background. 该新的单选按钮不适用于垂直对齐的单选按钮,除非它们的宽度受到限制,并且由于背景被图形占用,因此不能有单独的背景。 It'll also look terrible with text in it, but this works for me for now. 里面的文字看起来也很糟糕,但这对我来说还是可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM