Question

I'm trying to get a form such a way that on each row the TextView is on the left and the EditText is on the right.

The below code works fine for one row, however,r if I have multiple rows then it doesn't draw TextView and EditText on each row but instead tries to jam everything together.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dip"
    android:background="#FFFFFF"
    >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Full Name"
        android:textSize="20dp"
        android:layout_weight="1"
        android:textColor="#000000"
 />

    <EditText
        android:id="@+id/name"
        android:gravity="center"
        android:hint="John Doe"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Age"
        android:textSize="20dp"
        android:layout_weight="1"
        android:textColor="#000000"
        />

    <EditText
        android:id="@+id/age"
        android:gravity="center"
        android:hint="age"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />

</LinearLayout>

This is what it looks like

enter image description here

Était-ce utile?

La solution

There are so many ways to achieve the Layout which you been looking for. You were on track by adding layout_weight="1" in Views. Here's the one which uses your Layout only with little bit of orientation changes.

Explaination:

LinearLayout  //ParentView Orientation - Vertical
     LinearLayout //childView Orientation - Horizontal
        TextView & EditText // Taking equal space with help of layout_weight="1"
     LinearLayout //childView Orientation - Horizontal
        TextView & EditText // Taking equal space with help of layout_weight="1"

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip"
android:background="#FFFFFF"
>
    <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     >
       <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Full Name"
        android:textSize="20dp"
        android:layout_weight="1"
        android:textColor="#000000"
        />

       <EditText
        android:id="@+id/name"
        android:gravity="center"
        android:hint="John Doe"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
       />
    </LinearLayout>
    <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     >
     <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Age"
      android:textSize="20dp"
      android:layout_weight="1"
      android:textColor="#000000"
      />
     <EditText
      android:id="@+id/age"
      android:gravity="center"
      android:hint="age"
      android:layout_width="fill_parent"
      android:layout_weight="1"
      android:layout_height="wrap_content"
      />
    </LinearLayout> 
</LinearLayout>

Autres conseils

// try this way,hope this will help you...

<?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:padding="5dp"
    android:background="#FFFFFF">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.40"
                android:text="Full Name"
                android:textSize="20sp"
                android:gravity="right"
                android:textColor="#000000"/>

            <EditText
                android:id="@+id/name"
                android:gravity="center"
                android:hint="John Doe"
                android:layout_width="0dp"
                android:layout_weight="0.60"
                android:layout_height="wrap_content"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_marginLeft="5dp">
            <TextView
                android:layout_width="0dp"
                android:layout_weight="0.40"
                android:layout_height="wrap_content"
                android:text="Age"
                android:gravity="right"
                android:textSize="20sp"
                android:textColor="#000000"/>

            <EditText
                android:id="@+id/age"
                android:gravity="center"
                android:hint="Age"
                android:layout_width="0dp"
                android:layout_weight="0.60"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top