简体   繁体   中英

wrap_content is not working in button height android

"wrap_content" is not working in my button, it currently looks like this:

 now                        want
 ____________
|            |           _____________
|   aaaaaa   |     =>   | aaaaaaaaaaa |
|____________|           -------------

and the xml for my buttons :

 <Button 
     android:layout_gravity="center"
     android:id="@+id/input_expend"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"                               
     android:textSize="12sp"
     android:text="aaaaaaa"                         
     android:background="@drawable/btn_01"/>
 <Button 
     android:layout_gravity="center"
     android:id="@+id/input_expend"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"                               
     android:textSize="12sp"
     android:text="aaaaaaa"                         
     android:background="@drawable/btn_02"/> 

How can I solve this problem? Thanks!

Button is a View and the android:minHeight attribute is set 48dip default. You can set minHeight lower than your actual text 'aaaaa' such as 1dp. Then wrap_content will work. But I don't think this is recommended.

One solution is to add android:padding="@null" to the button in your XML. This will override any existing padding being enforced.

There is also an issue I've come across where a button will not shrink smaller than the padding defined in the default button style. I highlighted this in my own question some time ago but haven't had an answer that resolves it yet.

Changing the background didn't work, as well as padding="@null".

I ended up using a TextView because my button was simple enough for it to suffice:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Join"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="5dp"
        android:textSize="@dimen/xsmall_text_size"
        android:textColor="@drawable/button_text_selector"
        android:background="@color/turquoise"
        android:textStyle="bold"
        android:onClick="buttonOnClick"
        android:clickable="true"/>

wrap_content will not shrink a button smaller than the background. If you are already using a nine patch for the background, then cut back the size of the regions to the smallest they can be. If there's not a gradient, then you only really need about two pixels to define the stretchable regions and then make sure the central area is as small as it can be. Use the padding (right and bottom lines on ninepatch) to allocate the padding.

If you're using a drawable image (not a nine-patch or xml drawable) then make it a nine patch drawable

If you don't want to solve the problem in the image, you will need to set the layout_height to a fixed value to force the image to shrink (ie don't use wrap_content)

背景正在添加不需要的填充

Adding just minHeight doesn't work. You also need to set verticalPadding to 0dp

<Button
        android:id="@+id/button1"
        style="@style/Widget.MaterialComponents.Button.TextButton"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="1dp"
        android:paddingVertical="0dp"
        android:textSize="14sp"
        android:text="Login"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM