0

I have three identical buttons in one linear layout.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_weight="1"
        android:background="@drawable/button_clear"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Kamera öffnen" />
    <Button
        android:layout_weight="1"
        android:background="@drawable/button_clear"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Bild hochladen" />
    <Button
        android:layout_weight="1"
        android:background="@drawable/button_clear"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Abbrechen" />
</LinearLayout>

If I turn my device from horizontal to vertical, the text of the first two buttons need a wordwrap. The problem is, that the buttons change their height -> the two buttons with wordwrap have a lower height than the third button with only one text line.

Horizontal view: enter image description here

Vertical view: enter image description here

Why is that happening and how can I prevent it?

1 Answer 1

0

Hope this following xml will resolve your problems.

<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="3">
<Button
    android:layout_weight="1"
    android:background="@drawable/button_clear"
    android:layout_width="0dp"
    android:layout_height="60dp"
    android:text="Kamera öffnen" />
<Button
    android:layout_weight="1"
    android:background="@drawable/button_clear"
    android:layout_width="0dp"
    android:layout_height="60dp"
    android:text="Bild hochladen" />
<Button
    android:layout_weight="1"
    android:background="@drawable/button_clear"
    android:layout_width="0dp"
    android:layout_height="60dp"
    android:text="Abbrechen" />

Use weightSum on root layout while your are using layout_weight coz it will make conflict when you have more view or complex view. You should also use layout_width/height="0dp" depending on root layout orientation for getting the advantage of layout_weight. Because you trying to control your view using weight. Thank you

1
  • Thanks for your answer, but it does not solve the problem
    – Aiko West
    Apr 23, 2018 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.