Tanvir
Tanvir

Reputation: 1673

Weight in Vertical Linear Layout

I m confused about weight in vertical linear layout. I can use the weight well in horizontal linear layout but how to use it in vertical linear layout. I have a relative layout which contains a liner layout(vertical) that has some text-view and text-fields.I want the following arrangement.

Red line=relative layout,Green Line=Linear Layout(vertical),blue=textview,yellow=texfields

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal" />
    </LinearLayout>
</RelativeLayout>

Upvotes: 3

Views: 13746

Answers (5)

Gal Rom
Gal Rom

Reputation: 6461

In my case , after alittle games with vertical weight issue , i understood that android will wrap around the content of a ViewGroup and even if you set it with a weight that is bigger from the size of its content it will wrap around the content and give the extra space to the other views.

The solution is to add margin Top or Bottom depend on your needs .

Upvotes: 0

Eligos
Eligos

Reputation: 1132

I'd recommend using a TableLayout for this. If I've got this right, you're trying to apply weighting horizontally so you have regular columns in your LinearLayout. Assuming that's right, then you'll never do it like this.

LinearLayout arranges all elements either horizontally or vertically within itself. It can't do it both ways. So you'd need a separate row container in any case to achieve your goal.

TableLayout is not only designed with that intrinsic row container, but also has built in column support for just this sort of layout without requiring weighting to manage.

Upvotes: 0

mukesh
mukesh

Reputation: 4140

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >
     <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:layout_margin="10dp">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal" />

    </LinearLayout>
    <LinearLayout

        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal" />

    </LinearLayout>
     <LinearLayout

        android:id="@+id/linearLayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal" />


    </LinearLayout>
     <LinearLayout

        android:id="@+id/linearLayout4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal" />


    </LinearLayout>
    </LinearLayout>
</RelativeLayout>

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

- First of all you can directly have that arrangement in the Relative layout without the use of LinearLayout.

- Secondly if you want to have a child layout in the Relative layout, then its better to use TableRow in your Relative layout, and arrange the elements in it, just as u have shown.

Upvotes: 1

Ion Aalbers
Ion Aalbers

Reputation: 8030

If you are using the layout_weigth parameter, you should have parameter related to the orientation of the linear layout on match_parent

so:

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"

Upvotes: 3

Related Questions