wrap_content is not working in button height android

前端 未结 6 935
灰色年华
灰色年华 2020-12-31 01:40

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

 now                        want
 ____________
|            |           _________         


        
相关标签:
6条回答
  • 2020-12-31 02:19

    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)

    0 讨论(0)
  • 2020-12-31 02:21

    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"/>
    
    0 讨论(0)
  • 2020-12-31 02:27

    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"/>
    
    0 讨论(0)
  • 2020-12-31 02:30

    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.

    0 讨论(0)
  • 2020-12-31 02:36

    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.

    0 讨论(0)
  • 2020-12-31 02:39

    The background is adding unwanted padding

    0 讨论(0)
提交回复
热议问题