Android的 - 在Button 9patch填充 [英] Android - 9patch padding on a Button
问题描述
我创建了一个带有9patch图像作为背景的按钮。在9patch像我用的底部和右线指定内容区域/填充,按钮上文本的位置。这适用于一个TextView,但按钮上的内容行似乎对文本位置没有影响。设置paddingTop/paddingBottom会似乎也没有任何效果 - 只有填充有任何影响
I created a button with a 9patch image as a background. In the 9patch image I used the bottom and right lines to specify the content area/padding, to position the text on the button. This works well on a TextView, but on a Button the content lines seem to have no effect on the text position. Setting "paddingTop"/"paddingBottom" also seem to have no effect - only "padding" has any effect.
这是我的布局XML:
<Button
android:layout_width="450dp"
android:layout_height="198dp"
android:text="Some text here"
android:gravity="center"
android:background="@drawable/my_button"/>
有什么办法来设置填充?也许我可以用不同的视图,而不是一个按钮?例如,我可以尝试只使用一个TextView并使其点击,但我不知道这有什么其他的副作用。
Is there any way to set the padding? Maybe I could use a different View instead of a Button? For example, I could try to just use a TextView and make it clickable, but I'm not sure what other side-effects this will have.
推荐答案
设置明确的android:填充=@空
解决我的问题。
Explicitly setting android:padding="@null"
solved my issue.
问题似乎是一个按钮(或在我使用的主题风格之一)是设置填充,它覆盖在按钮的所有其他填充 - 填充背景绘制,以及 paddingTop
, paddingLeft
等。
The issue seemed to be that a Button (or one of the styles in the theme I'm using) is setting the padding, which overrides all other padding in the button - padding in the background drawable, as well as paddingTop
, paddingLeft
etc.
我的按钮布局,现在这样定义的:
My button layout is now defined like this:
<Button
android:layout_width="450dp"
android:layout_height="198dp"
android:text="Some text here"
android:gravity="center"
android:padding="@null"
android:background="@drawable/my_button"/>
我的IDE(IntelliJ IDEA的11.1)拿起 @null
为错误,即使它编译。将其设置为 -1px
还曾。
My IDE (IntelliJ IDEA 11.1) picked up the @null
as an error, even though it compiled. Setting it to -1px
also worked.
这篇关于Android的 - 在Button 9patch填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!