0

Consider the following layout: a horizontal LinearLayout with 2 buttons with the text "Push me", and an ImageButton with a small envelope icon (for example, to send an email).

I want the ImageButton's width to be set to "wrap_content" so it's only as large as the envelope's size, and then I want the other two buttons to evenly split the remainder of the screen evenly. How can it be done?

2 Answers 2

1

layout_weight specifies how much of the extra space in the layout to be allocated to the View. So, for your envelope ImageButton set the android:layout_width="wrap_content". And for your remaining 2 buttons set android:layout_width="0dp" and add android:layout_weight="0.5" to both buttons.

0

Try this

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <ImageButton
    android:id="@+id/ib"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>

  <LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    >

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Push me"/>

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Push me"/>

  </LinearLayout>
</LinearLayout>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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