0

my requirement is to make 10 layouts in a single layout . Each layout should be 50 % horizontally and 20% Vertically.

Current my code can make horizentally 50% but how to make same layouts to 20% vertical.

Can it be possible.

Code is

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

<LinearLayout android:layout_width="0dip"
    android:layout_height="wrap_content" android:orientation="horizontal"
    android:id="@+id/linearLayoutouter" android:layout_weight=".5"
    android:background="@android:color/holo_green_dark">

    <Button android:text="Button" android:id="@+id/button11"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center">
    </Button>

</LinearLayout>

<LinearLayout android:layout_height="wrap_content"
    android:id="@+id/linearLayout1" 
    android:orientation="vertical"
    android:layout_width="0dip" 
    android:layout_weight=".5"
    android:background="@android:color/holo_orange_light">
    <Button android:text="Button" 
        android:id="@+id/button1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center">
    </Button>
    <Button android:layout_width="wrap_content" 
        android:id="@+id/button2"
        android:layout_height="wrap_content" 
        android:text="Button"
        android:layout_gravity="center"></Button>

</LinearLayout>

user3698957
  • 35
  • 1
  • 7

3 Answers3

0

You can do that with nested layouts, like a grid.

<!-- Row 0 -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:orientation="vertical">
    <!-- Row 0: Column 0-->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        android:orientation="horizontal">
        <Button ... />

    </LinearLayout>
    <!-- Row 0: Column 1-->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        android:orientation="horizontal">
        <Button ... />

    </LinearLayout>
</LinearLayout>
...

...
<!-- Row 4 -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:orientation="vertical">
    <!-- Row 4: Column 0-->
    ...
</LinearLayout>

It looks a bit odd, maybe you could consider using a ListView or any other strategy that fits better.

Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20
0

You can use weightSum property like below

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="5">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:weightSum="2"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"></LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"></LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:weightSum="2"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"></LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"></LinearLayout>
    </LinearLayout>

    <!-- 3 more rows here -->

</LinearLayout>

Outer weightSum is used for 5 rows. And inner weightSum is used for 2 columns

Vinoth Kannan
  • 242
  • 5
  • 16
0

u should add another child layout in your linear layout to avoid nested weight. and check this What is android:weightSum in android, and how does it work?

try this..hope works fo u..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/linearLayoutouter"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:background="#ff0000"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Button" >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        android:background="#000000"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:orientation="vertical"
            android:weightSum="10" >

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

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

            <Button
                android:id="@+id/button5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button6"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button7"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button8"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button9"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button10"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <!-- added 8 mote view with weight 1 -->
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
Community
  • 1
  • 1
M S Gadag
  • 2,057
  • 1
  • 12
  • 15