Dunkey
Dunkey

Reputation: 1922

Align textviews in linearlayout

This is what I want to achieve:

here

main.xml

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" >



    <TextView
        android:id="@+id/tvNameContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="2dip"
        android:layout_weight="0.29"
        android:textColor="#FFF"
        android:gravity="center_horizontal"
        android:text="@string/fullname"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvDiagnosisContainer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dp"
        android:layout_weight="0.57"
        android:gravity="center_horizontal"
        android:text="@string/diagnosis"
        android:textColor="@color/white"
        android:textStyle="bold" />

  </LinearLayout>

custom_row.xml

<?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="vertical"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" >

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

<TextView
    android:id="@+id/fullname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:layout_weight="0.29"
    android:text="text"
    android:textColor="@color/black" />

<TextView
    android:id="@+id/lname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.29"
    android:text="text"
    android:textColor="@color/black" />

<TextView
    android:id="@+id/diagnosis"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.57"
    android:text="@string/diagnosis"
    android:padding="5dp"
    android:textColor="@color/black" />

 </LinearLayout>

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

<TextView
    android:id="@+id/lastffupcontainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lastffup"
    android:padding="5dp"
    android:textColor="@color/black" />

<TextView
    android:id="@+id/lastffup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lastffup"
    android:padding="5dp"
    android:textColor="@color/black" />

 </LinearLayout>

 </LinearLayout>

This is the result: enter image description here

But I can't seem to make those spaces between the textviews. Any ideas?

Upvotes: 0

Views: 105

Answers (2)

nomachinez
nomachinez

Reputation: 521

You can use Android's new fancy GridLayout (http://developer.android.com/reference/android/widget/GridLayout.html or http://developer.android.com/reference/android/support/v7/widget/GridLayout.html) to get exactly what you're looking for.

Try and replace your LinearLayout tags with GridLayout instead and it just might work for you. (make sure you add the support library to your project if you need to support devices < api 14).

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

Correct me if I'm wrong but it looks like the only issue you are having is having the "Diagnosis" TextView in the ListView spaced more to the right. For this I would change that LinearLayout to a RelativeLayout. Then you can give it the property android:layout_alignParentRight="true" and add in some android:marginRight="some dp" if needed.

Upvotes: 1

Related Questions