Button overlay does not work without background

Today I ran into a weird layout problem, and I did not find a useful 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 at 20dp from the left of the field to the border of the button, after which I set paddingLeft to the button, but it does not work. By chance, I set the background color for the button, and the cushion works like a charm. Anyone can help me explain this.

Layout 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" /> 

Thanks everyone!

+6
source share
3 answers

Setting minWidth and minHight seems to do the trick, allowing the correct padding and margins to work with or without the background.

 <Button android:minHeight="0dp" android:minWidth="0dp" ... 

As for the background, it does something with how the add-on works ... I think this is due to 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()); } 

fooobar.com/questions/61034 / ...

+11
source

You can try this

Instead of using a button, you can use Textview, it works the way you want

  <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableRight="@drawable/right_arrow" android:gravity="center_vertical" android:text="Button" android:paddingLeft="20dp" android:paddingRight="20dp" android:textColor="#ffffff00" /> 

Hope this works for you.

+1
source

Add this

 android:drawablePadding="20dp" 
0
source

Source: https://habr.com/ru/post/985319/


All Articles