Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android layout weight %

I am trying to messing around with the Android layout weight attributes. I want a vertical layout with "3 child layouts" The first will take 25% of the space, the second 50%, and the last 25% of the space. When I try to add to the last layout, everything just not working. What should be the weight for each of these layouts?

I get the weight attributes to work fine with just 2 layouts/elements and understand how it works but when I try with 3 I get a problem.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:orientation="horizontal"
    android:weightSum="100" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:orientation="vertical"
        android:layout_weight="25" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:orientation="vertical"
       android:layout_weight="50" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:orientation="vertical"
        android:layout_weight="25" >
    </LinearLayout>
</LinearLayout>
like image 550
Ukjent Avatar asked Aug 04 '26 01:08

Ukjent


2 Answers

try using this code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:weightSum="100">
  <LinearLayout
     android:layout_width="0dp"
     android:layout_height="100dp"
     android:orientation="vertical"
     android:layout_weight="25">
  </LinearLayout>
  <LinearLayout
     android:layout_width="0dp"
     android:layout_height="100dp"
     android:orientation="vertical"
     android:layout_weight="50" >
  </LinearLayout>
  <LinearLayout
     android:layout_width="0dp"
     android:layout_height="100dp"
     android:orientation="vertical"
     android:layout_weight="25">
  </LinearLayout>
</LinearLayout>
like image 190
jase Avatar answered Aug 06 '26 13:08

jase


Make sure that the android:width attribute is set to 0dp for all child layouts, and then make sure that you have a weight specified. If you want a more detailed explanation, you will need to paste your code.

like image 39
javram Avatar answered Aug 06 '26 15:08

javram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!