berimbolo
berimbolo

Reputation: 3829

Android - specifying layout_width when using layout_weight

When I am specifying a weight between say and image view and a linear layout which are contained within the same linear layout what should I be setting the layout_width values to? It seems like it shouldnt be set because I am saying I want the other layout to take up 90% of space and the image view to take 10% of space.

Also it seems like I can set the image view to have 10% space and the layout to have 90% space but I can still specify a value in dp's for the image view?

In this instance should I just set the value in dp's on the image and then specify a weight of 1 for the other layout?

Thanks

Upvotes: 0

Views: 910

Answers (2)

Ibukun Muyide
Ibukun Muyide

Reputation: 1298

When using weight in layout. you will want to do this.

The parent layout will have a weightSum=10 using a value of 10, but it could be any value we want. for the children layout. all should belayout_width="fill_parent"then you can then set the layout_weight="2" for one of the child and then layout_weight="8" for the other child

something like this

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10">


<imageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="8"
    />

<LinearLayoout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2" />

Upvotes: 1

Rahul Tiwari
Rahul Tiwari

Reputation: 6968

In this case your layout_width should be 0dp. you will also get a lint warning if will define width other than 0dp with a weight param.

refer this for more details.

Upvotes: 1

Related Questions