Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does android:layout_weight work?

Tags:

android

When I have the following, it shows top layout with four colors has much smaller area than the bottom layout area.

According to this documentation, when you add more to layout_weight, it should increase the area, but it decreases in the code.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent">    <LinearLayout       android:orientation="horizontal"       android:layout_width="fill_parent"       android:layout_height="fill_parent"       android:layout_weight="4">       <TextView           android:text="red"           android:gravity="center_horizontal"           android:background="#aa0000"           android:layout_width="wrap_content"           android:layout_height="fill_parent"           android:layout_weight="3"/>       <TextView           android:text="green"           android:gravity="center_horizontal"           android:background="#00aa00"           android:layout_width="wrap_content"           android:layout_height="fill_parent"           android:layout_weight="1"/>       <TextView           android:text="blue"           android:gravity="center_horizontal"           android:background="#0000aa"           android:layout_width="wrap_content"           android:layout_height="fill_parent"           android:layout_weight="1"/>       <TextView           android:text="yellow"           android:gravity="center_horizontal"           android:background="#aaaa00"           android:layout_width="wrap_content"           android:layout_height="fill_parent"           android:layout_weight="1"/>   </LinearLayout>    <LinearLayout     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_weight="1">     <TextView         android:text="row one"         android:textSize="15pt"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_weight="1"/>     <TextView         android:text="row two"         android:textSize="15pt"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_weight="1"/>     <TextView         android:text="row three"         android:textSize="15pt"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_weight="1"/>     <TextView         android:text="row four"         android:textSize="15pt"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_weight="1"/>   </LinearLayout>  </LinearLayout> 
like image 374
shin Avatar asked Jun 21 '10 11:06

shin


People also ask

What is layout_weight and weightSum in android?

Answer: Per documentation, android:weightSum defines the maximum weight sum, and is calculated as the sum of the layout_weight of all the children if not specified explicitly. Let's consider an example with a LinearLayout with horizontal orientation and 3 ImageViews inside it.

What is android gravity?

android:gravity is used to specify how to place the content of the object within the object itself. In another word, android:gravity is used to specify the gravity of the content of the view. android:layout_gravity is an attribution the child can supply to its parent, to specify the gravity the view within its parents.

What is 0dp android?

This answer is not useful. Show activity on this post. Well, height or width when set to "0dp", are mostly used in combination with "weight". e.g. you want to fill all the available space for height: android:layout_height = "0dp" android:layout_weight = "1.0" Follow this answer to receive notifications.

What is a FrameLayout in android?

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.


2 Answers

I had the same problem. The trick is not to use "wrap_content" or "fill_parent" for the controls you are setting a weight to. Instead set the layout_height to 0px (when inside a vertical layout) and then the child controls will get proportioned per the weight values.

like image 108
kbriggs Avatar answered Sep 22 '22 19:09

kbriggs


If you get the error as

error

Suspicious size: this will make the view invisible, probably intended for layout ...

remember to set the correct parameter on android:orientation in parent

like image 39
pedroca Avatar answered Sep 21 '22 19:09

pedroca