Button does not wrap content

I use a simple horizontal LinearLayout to place 3 buttons next to each other, with the same width. I achieve this with the following xml text:

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:baselineAligned="false" > <Button android:id="@+id/help" style="android:buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/help" /> <Button android:id="@+id/close" style="android:buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/closemultigame" /> <Button android:id="@+id/ok" style="android:buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/restartmultigame" /> </LinearLayout> 

Now the layout is as follows: screenshot from designer

The layout_height button is defined as wrap_content, but the right-most button does not carry its contents. It looks different on different devices, on some it looks good. What I really would like to achieve is three buttons, the same width and the same height, each with its own text, centered both horizontally and vertically. What approach would you suggest?

ADDED: entire layout on request:

 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/activity_horizontal_margin" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:drawableLeft="@drawable/matchandfit" android:drawablePadding="@dimen/activity_horizontal_margin" android:gravity="center_vertical" android:padding="@dimen/activity_horizontal_margin" android:text="@string/multigameover" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/status" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/multigameresult" android:textAppearance="?android:attr/textAppearanceMedium" /> <LinearLayout android:id="@+id/masterresult" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/masterresultinfo" android:textAppearance="?android:attr/textAppearanceMedium" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:baselineAligned="false" > <Button android:id="@+id/help1" style="android:buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="@string/help" /> <Button android:id="@+id/close1" style="android:buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="@string/closemultigame" /> <Button android:id="@+id/ok1" style="android:buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="@string/restartmultigame" /> </LinearLayout> </LinearLayout> </LinearLayout> </ScrollView> 

As Vasim Ahmed noted, this is due to ScrollView. Now I use ScrollView to make sure the buttons remain accessible even in the landscape on small devices. (Layout for dialogue). Is there any other way to always keep the buttons accessible / visible?

Workaround: The solution, or the best workaround I have found, is to add a TextView at the end of the included LinearLayout. This TextView is vertically cropped at about half the height, but there is no text in it, so no one will notice. Adding some addition to the bottom of the attached LinearLayout did not help

+6
source share
5 answers

Again and again, I noticed that the Button class does not adjust the size of the text, even if you specify android:layout_height="wrap_content" .

You can do one of two things:

1. You can manually set the text size or the width of your Button until the text is set correctly (simulates the appearance of wrap_content ).

2. Instead, use a TextView (which correctly wraps the text when you specify android:layout_height="wrap_content" ), and you set onClickListener to the TextView to mimic the behavior of the button.

+3
source

Do not flip the height of the button, but use fill_parent to increase the height of the buttons with each other.

Example:

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:orientation="horizontal" android:baselineAligned="false" > <Button android:id="@+id/help" style="android:buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="hilfe" /> <Button android:id="@+id/close" style="android:buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="beenden" /> <Button android:id="@+id/ok" style="android:buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="neue Pundo" /> </LinearLayout> 

result:

enter image description here

+1
source

I just tried and it worked:

1) Define in .. / res / values ​​/strings.xml:

Line1Line1 \ nLine2Line2

2) Refer to it in the layout file:

 <Button android:id="@+id/btn_multilines" android:text="@string/multilines" android:layout_height="wrap_content" android:layout_width="fill_parent"> </Button> 
0
source

Try using android: layout_width = "wrap_content" instead of android: layout_width = "0dp" and see id

-1
source

The workaround to this is to set minHeight of Button like this

 android:minHeight="1dp" 

He should work

-1
source

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


All Articles