Jeff Axelrod
Jeff Axelrod

Reputation: 28188

Can only LinearLayout specify a fixed-size element with an adjacent element that uses the remaining space?

I'd like to have two adjacent views, a first that is a fixed size and a second that adjacent to the first that uses the remaining space.

I could easily do this with LinearLayout and weights, but I would like to avoid the "nested weights are bad for performance" problem.

Is there another layout type that can accomplish the equivalent? Please provide an example if so.

Upvotes: 2

Views: 3448

Answers (3)

user
user

Reputation: 87064

A RelativeLayout could do what you want, for example:

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

    <Button
        android:id="@+id/button1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Button"
        android:background="#99cc00" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/button1"
        android:text="Button" 
        android:background="#0077cc"/>

</RelativeLayout>

The first Button will be 200dp in width and the second will stretch to fill the rest of the parent's remaining width.

You could also use a RelativeLayout to split two views in equal sizes to avoid having double weights on some layouts.

Upvotes: 3

drewhannay
drewhannay

Reputation: 1020

I believe this could be done with a RelativeLayout. Example:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/button">
       ...
    </LinearLayout>

</RelativeLayout>

Upvotes: 2

jaredzhang
jaredzhang

Reputation: 1158

You can try

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="view with fixed size " />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="view with remaining space" />

</LinearLayout>

this is how weight works in LinearLayout:

At first, it will deduct the fixed dimension, then according to the weight, divide the available space, assign to the views which specify the weight attribute 

Upvotes: -1

Related Questions