按钮缩放到背景图像而不是内容
我需要做的是让背景图像恰好填充按钮的自然大小,就像没有指定背景并显示默认的灰色背景图像时一样。但相反,我的按钮会放大到背景图像的大小而不是文本的大小。
之前有一个问题,但没有人找到解决方案。
按钮:
<Button
android:id="@+id/morebtn"
style="@style/CustomButton"
android:text="More" />
我有这个自定义按钮样式(灰色按钮是 9 补丁):
<resources>
<style name="CustomButton" parent="@android:style/TextAppearance">
<item name="android:textColor">#FFFFFF</item>
<item name="android:layout_marginLeft">10dip</item>
<item name="android:layout_marginRight">10dip</item>
<item name="android:layout_marginBottom">10dip</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_centerInParent">true</item>
<item name="android:background">@drawable/greybutton</item>
<item name="android:textSize">20sp</item>
</style>
</resources>
现在它填充尽管被告知要wrapp_content,但还是沿屏幕宽度方向向上移动。当我从按钮样式中删除 android:background 时,按钮会缩小以按预期换行文本。
有谁明白为什么我的背景图像的行为与默认按钮背景不同?
What I need to do is have the background image just fill up the natural size of the button, as when no background is specified and the default grey background image is shown. But instead, my button scales up to the size of the background image rather than the text.
There was a previous question, but nobody had a solution.
The button:
<Button
android:id="@+id/morebtn"
style="@style/CustomButton"
android:text="More" />
I have this custom button style (greybutton is a 9-patch):
<resources>
<style name="CustomButton" parent="@android:style/TextAppearance">
<item name="android:textColor">#FFFFFF</item>
<item name="android:layout_marginLeft">10dip</item>
<item name="android:layout_marginRight">10dip</item>
<item name="android:layout_marginBottom">10dip</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_centerInParent">true</item>
<item name="android:background">@drawable/greybutton</item>
<item name="android:textSize">20sp</item>
</style>
</resources>
Right now it fills up the screen widthwise, despite being told to wrap_content. When I remove android:background from the Button style, the button shrinks down to wrap the text as expected.
Does anyone see why my background image isn't behaving like the default Button background?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不使用 ImageButton 来代替:
然后您可以使用 scaleType 属性调整缩放比例。
唯一的缺点是您无法设置任何文本。
Why don't you use ImageButton instead:
then you can adjust the scaling with the scaleType attribute.
The only downside is you can't set any text.
该死的。我有一个 hdpi 可绘制对象尚未转换为 9patch,并且由于某种原因,即使在我的 mdpi 屏幕上,它也被应用,而不是 mdpi 可绘制对象文件夹中的那个。毕竟背景缩放实际上确实按预期工作。对不起!
(因此,如果您正在浏览此问题并遇到类似问题,请检查您的可绘制文件夹,以确保您正在绘制您认为的内容。另外:可能希望确保您的九个补丁中的内容边界是适当的请注意,它们与可拉伸边界不同。)
Damnit. I had an hdpi drawable that hadn't been converted to 9patch, and for some reason that was getting applied instead of the one in the mdpi drawables folder even on my mdpi screen. The background scaling actually does work as expected after all. Sorry!
(So if you're browsing this question with a similar problem, check your drawables folder to make sure you're drawing what you think you are. Also: may wish to be sure the content boundaries in your nine-patch are of the appropriate size. Note that they aren't the same as the stretchable boundaries.)
您可以尝试使用dip 对按钮的高度和宽度进行硬编码。如果您需要更改按钮上的文本,这将是一个问题,但它应该可以限制按钮扩展到图像的大小。当然,大图像会使按钮变大..宽度/高度是wrap_content,并且图像显然是内容的一部分...尝试对宽度/高度参数进行硬编码。
You could try hard coding the height and width of the buttons using dip. That will be an issue if you need the Text on the buttons to change but it should work to restrain the button from expanding to the size of the image. Of course a large image will make the button larger.. the width / height is wrap_content, and the image is clearly part of the content... try hardcoding width / height parameters.
将这两项添加到您的 CustomButton 样式中
Add these two items to your CustomButton style