今天我遇到了一个奇怪的布局问题,我没有从谷歌找到任何有用的答案.
在我的布局上,我有一个按钮,左边是文本,右边是图标.我希望文本是20dp左边距到按钮的边框然后我将paddingLeft设置为按钮,但它不起作用.偶然的机会,我为按钮设置了背景颜色,填充就像魅力一样.任何人都可以帮我解释这件事.
布局如下
<Buttonandroid:layout_width="fill_parent"
android:drawableRight="@drawable/right_arrow"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_height="72dp"
android:text="Button"
android:id="@+id/btn"
android:gravity="center_vertical"
android:fontFamily="roboto regular"
android:textColor="#ffffff00"
style="@android:style/Widget.DeviceDefault.Button.Borderless" />
谢谢你们!
解决方法
设置minWidth和minHight似乎可以使填充和边距在有或没有背景的情况下正常工作.
<Button
android:minHeight="0dp"
android:minWidth="0dp" ...
至于为什么背景对填充的工作原理做了什么……我认为它与View.java中的这段代码有关
protected int getSuggestedMinimumWidth() {
return (mBackground == null) ? mMinWidth : max(mMinWidth,mBackground.getMinimumWidth());
}
protected int getSuggestedMinimumHeight() {
return (mBackground == null) ? mMinHeight : max(mMinHeight,mBackground.getMinimumHeight());
}
https://stackoverflow.com/a/20323723/4401507