2

I want to make a LinearLayout like this by percent enter image description here

each rectangle is a LinearLayout

is that possible to make and how ?

Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
HamidTB
  • 1,381
  • 3
  • 11
  • 15
  • possible duplicate of [Linear Layout and weight in Android](http://stackoverflow.com/questions/2698817/linear-layout-and-weight-in-android) – davidcesarino Jan 03 '14 at 17:06

3 Answers3

4

Check this sample xml...this will helpfull to understand the weightSum and layout_weight in the LinearLayout

Check this SO

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

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:text="Button20" />

<Button
    android:id="@+id/button1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:text="Button20" />

<Button
    android:id="@+id/button3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:text="Button20" />

<Button
    android:id="@+id/button4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="40"
    android:text="Button40" />

Community
  • 1
  • 1
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
  • use android:layout_width="0dp" for each Button. If the orientation is horizontal. – Amit Gupta Jan 03 '14 at 12:25
  • Oops Sorry!! Forgotten!! Thanks:) – Jagadesh Seeram Jan 03 '14 at 12:29
  • @Namecan'tbedisplayed Please flag the question as a duplicate next time. I understand your good will to answer and help, but please try to keep things organized whenever you see a satisfactory answer elsewhere, as your own answer says when linking to it. – davidcesarino Jan 03 '14 at 17:09
1

Use this One:

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

  <Button
    android:id="@+id/button1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:text="20%" />

  <Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:text="20%" />

  <Button
    android:id="@+id/button3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:text="20%" />

  <Button
    android:id="@+id/button4"
    android:layout_width="0dp"
    android:layout_gravity="center"
    android:layout_height="wrap_content"
    android:layout_weight="40"
    android:text="40%" />

</LinearLayout>
Piyush
  • 18,895
  • 5
  • 32
  • 63
0

You need to make each cell's width equal to match_parent and then play with the layout_weight value for each child. As layout_weight specifies size ratio between multiple elements, i.e.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141