2017-05-13 4 views
0

Ich verwende zwei Textansichten in meinem Listenelement, die in einer Listenansicht einer Android-Anwendung verwendet werden.Ausrichten von zwei Textansichten horizontal in einem Listenelement

Die Textansichten werden jedoch in jedem Listenelement untereinander angezeigt und ich möchte sie in jedem Element horizontal anzeigen. Ist die Verwendung eines linearen Layouts hilfreich? Oder ist es möglich, ein Relatives Layout zu verwenden?

android: layout_alignRight = "@ + id/lnummer" Die Verwendung dieser in meiner zweiten Textansicht führte dazu, dass meine Textelemente vertikal am linken Ende des Listenelements gedruckt wurden. In

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="@dimen/activity_horizontal_margin"> 

    <TextView 
     android:id="@+id/lnumber" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:paddingTop="6dip" 
     android:textColor="#5d5d5d" 
     android:textSize="16sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/lname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:textColor="#5d5d5d" 
     /> 

</RelativeLayout> 

Antwort

0

Relative Layout mehr Zeit in Anspruch nehmen als auf lineares Layout vergleichen so aufzublasen verwenden, wie Sie können als lineares Layout statt des relativen Layouts.

Lineares Layout bereits horizontal ausgerichtet, so dass Sie es direkt verwenden können. Und Sie können auch Gewichte geben.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:weightSum="2"> 

<TextView 
    android:id="@+id/lnumber" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:background="@android:color/black" 
    android:paddingBottom="2dip" 
    android:paddingTop="6dip" 
    android:text="Priyanka" 
    android:textColor="#ffffff" 
    android:textSize="16sp" 
    android:textStyle="bold" /> 

<TextView 
    android:id="@+id/lname" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:text="Madgundi" 
    android:background="@android:color/black" 
    android:paddingBottom="2dip" 
    android:paddingTop="6dip" 
    android:textColor="#ffffff" /> 

0

Gerade Attribut android:layout_toRightOf="@id/lnumber" und android:layout_alignBottom="@id/lnumber" auf Textview lname auszurichten es nach rechts von lnumber.

Versuchen Sie folgendes:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="@dimen/activity_horizontal_margin"> 

    <TextView 
     android:id="@+id/lnumber" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#5d5d5d" 
     android:textSize="16sp" 
     android:textStyle="bold" 
     android:text="First"/> 

    <TextView 
     android:id="@+id/lname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/lnumber" 
     android:layout_alignBottom="@id/lnumber" 
     android:layout_marginLeft="8dp" 
     android:textColor="#5d5d5d" 
     android:text="Second"/> 

</RelativeLayout> 

OUTPUT:

enter image description here

Verwandte Themen