Today I encountered a quit weird layout issue and I have not found out any helpful answer from Google.
On my layout, I have a button with text on the left and an icon on the right. I want the text to be 20dp left margin to the border of the button then I set paddingLeft to the button but it's not working. By chance, I set background color for the button and the padding works like charm. Anyone can help me explain this thing.
The layout is as below
<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" />
Thank you all!
Setting minWidth
and minHeight
seem to do the trick of getting the padding and margins to work properly with or without a background.
<Button
android:minHeight="0dp"
android:minWidth="0dp" ...
As far as why the background does anything to how the padding works... I think it has to do with this bit of code in 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With