2017-03-07 3 views
0

Ich habe eine TextView. Ich möchte alle TextView (Nr.123 ... & Sea View ...) nach links ausrichten. Ich weiß android:gravity="left"android:layout_gravity="left", aber es ist nicht für mein Problem geeignet.Wie richtet man alle TextView in Android?

Hier ist mein xml-Code;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

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

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Address " /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

     </LinearLayout> 

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

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Type "/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sea View, Graden View, Local" /> 

     </LinearLayout> 
</LinearLayout> 

Wie es mit linker Textansicht ausrichten? Wie unten bild.

enter image description here

Output Ergebnis;

enter image description here

+0

was ist Ihre Ausgabe? kannst du Screenshots teilen? und bitte aktualisieren Sie das übergeordnete Layout von LinearLayout in Ihrem Code-Snippet –

+1

zeigen Sie irgendwie, was Sie bewegen möchten, und in welcher Richtung. Es ist nicht klar. –

+0

siehe unten http://Stackoverflow.com/a/42641608/3513479 keine Notwendigkeit, ändern Sie Ihren Code nur eine Zeile setzen –

Antwort

2
beziehen

Verwenden Relative-Layout stattdessen zu erreichen, was Sie wollen,

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/address" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Address " /> 

     <TextView android:id="@+id/type" 
      android:layout_below="@+id/address" 
      android:layout_alignRight="@+id/address" 
      android:layout_width="match_parent" 
      android:gravity="left" 
      android:layout_height="wrap_content" 
      android:text="Type "/> 

     <TextView 
      android:layout_toRightOf="@+id/address" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

     <TextView 
      android:layout_alignBottom="@+id/type" 
      android:layout_toRightOf="@+id/type" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Sea View, Graden View, Local" /> 
    </RelativeLayout> 

enter image description here

+0

Oh !!! Ich habe vergessen, RelativeLayout zu verwenden. Danke. – WPG

0

Mit Hilfe von layout_weight und weighSum Sie dies erreichen, können Sie folgende Code-Schnipsel kann

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="2" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:text="Address" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_weight="1.8" 
      android:layout_height="wrap_content" 
      android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

    </LinearLayout> 

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_weight="0.2" 
      android:layout_height="wrap_content" 
      android:text="Type"/> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1.8" 
      android:text="Sea View, Graden View, Local" /> 

    </LinearLayout> 

</LinearLayout> 

für mehr Verständnis über Linearlayout und seine Attribute helfen Ihnen https://developer.android.com/reference/android/widget/LinearLayout.html

0

Verwenden layout_weight und weightSum Eigentum von Linearlayout, die Ihr Layout richtig ausgerichtet ist und reagiert machen.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

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

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="4" 
     android:layout_height="wrap_content" 
     android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="5" 
    android:layout_marginTop="16dp" 
    android:orientation="horizontal"> 

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

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="4" 
     android:text="Sea View, Graden View, Local" /> 

</LinearLayout> 

1

nichts mehr im Code zu tun, nur eine Zeile hinzufügen Um Ihre Adresse & Typ

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

statt

<TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:ems="5" 
     android:text="Address" /> 

entfernen android: layout_weight = "1"

Sie können Anstieg oder Abnahmeems Wert wie pro Ihre Anforderung

jetzt, was ems

EMS bedeutet, dass Sie sagen, in XML diese Layout-Datei ist mein fix Breite auf die Textgröße basiert:

wie wenn Sie Android: ems = "5" dann würde es verbrauchen 5Character

von Platz für Sie in Design. es bedeutet indirekt Sie die Breite Ihres Layout oder Texttext einstellen.

Enjoy Codierung;)

Verwandte Themen