Manuel Manuel - 2 months ago 9
Android Question

How to alight ListView to TextView

Hi Im having problem aligning data i show in a ListView to the Textview that Im using as a header.Im a newbie and i wanted originally use a LayoutTable but its complicated if you can give me an idea on that score will be great.

I want everything to be parallel to its header

See Xml below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<LinearLayout
android:orientation="horizontal"
android:id="@+id/linearlayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">


<TextView
android:id="@+id/w_r"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:orientation="vertical"
android:text="what"
android:textSize="20dp"
android:layout_marginLeft="10dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"

android:text="where"
android:id="@+id/f_l"
android:layout_marginLeft="50dp"
android:textSize="20dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="50dp"
android:orientation="vertical"
android:text="???"
android:id="@+id/w_f"
android:textSize="20dp" />


<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:orientation="vertical"
android:text="Storing"
android:id="@+id/S_r"
android:layout_marginLeft="55dp"
android:textSize="20dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="look"
android:id="@+id/looks"
android:textSize="20dp"
android:layout_marginLeft="50dp" />
</LinearLayout>



<ListView
android:id="@+id/listarecdatos"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_alignParentBottom="false"
android:layout_below="@+id/linearlayout2"
android:layout_marginTop="10dp"
android:footerDividersEnabled="false"
android:layout_marginLeft="10dp" />



Answer

Use weights for both layout containers, each columns must have weight of 1 in your header layout and in your item layout

EDIT - Add sample code

Your Activity/Fragment that will contain the header, and the view holder of each item layout should look like this

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

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

If you want to distribute in a horizontal then you should set layout_height="0dp" instead of layout_width

Comments