0

Par exemple, j'ai un ListView, et pour chaque ligne de données est comme ceComment aligner le texte dans la vue texte Android

Name: Mr.ABC 
Address: Flat A, 25/F, Block 2, 
USA.... 
Gender: Male 
Hobbies: Football, Baseball..... 
..... 

Et ce qui précède est la façon dont il regarde actuellement aime, j'utilise 4 regardez le texte pour représenter chaque donnée

<LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="9" 
     android:orientation="vertical" 
     android:paddingLeft="5dp" > 

     <TextView 
      android:id="@+id/resultName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/resultLang" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/resultType" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/resultOrg" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/resultYear" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16sp" /> 
    </LinearLayout> 

le problème est comment puis-je aligner le texte comme celui-ci?

Name: Mr.ABC 
Address: Flat A, 25/F, Block 2, 
     USA.... 
Gender: Male 
Hobbies: Football, Baseball..... 
     ..... 

Merci pour aider

Mise à jour

<TableLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="9" 
    android:paddingLeft="5dp" 
    android:shrinkColumns="*" 
    android:stretchColumns="*" > 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_weight="1" 
      android:text="@string/search_name" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/resultName" 
      android:layout_weight="1" 
      android:textSize="16sp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_weight="1" 
      android:text="@string/search_lang" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/resultLang" 
      android:layout_weight="1" 
      android:textSize="16sp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_weight="1" 
      android:text="@string/search_type" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/resultType" 
      android:layout_weight="1" 
      android:textSize="16sp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_weight="1" 
      android:text="@string/search_org" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/resultOrg" 
      android:layout_weight="1" 
      android:textSize="16sp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_weight="1" 
      android:text="@string/search_yr" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/resultYear" 
      android:layout_weight="1" 
      android:textSize="16sp" /> 
    </TableRow> 
</TableLayout> 

Le code ci-dessus est mis à jour, mais le problème est, car il est listview et je trouve que la largeur du côté gauche n'est pas égal. C'est quelque chose comme par exemple

Name: Mr.ABC 
Address: Flat A, 25/F, Block 2, 
     USA.... 
Gender: Male 
Hobbies: Football, Baseball..... 
     ..... 

Name:  Mr.Asfsafasfdfsd 
Address:  Flat V, 25/F, Block 2, 
      USA.... 
Gender:  Female 
Hobbies:  Basketball..... 
      ..... 

Name:  Mr.WQWRWEE 
Address: Flat V, 25/F, Block 2, 
      USA.... 
Gender: Male 
Hobbies: Baseball..... 
      ..... 
+1

Vous pouvez utiliser 8 textviews. Je ne pense pas que vous pourriez obtenir l'alignement désiré en utilisant seulement 4 textviews – novic3

+0

oui et en utilisant la mise en page de poids, mais comment puis-je définir le poids? merci – user782104

+0

par exemple. 1 pour le nom: et 9 pour le M. ABC? mais que faire si un écran d'appareil est trop petit pour montrer le nom? – user782104

Répondre

2
// Try this way,hope this will help you... 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="5dp" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/txtNameLable" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:layout_weight="0.25" 
      android:textSize="16sp" 
      android:text="Name : "/> 
     <TextView 
      android:id="@+id/txtNameValue" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:layout_marginLeft="3dp" 
      android:textSize="16sp" 
      android:text="Mr.ABC"/> 
     </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp"> 
     <TextView 
      android:id="@+id/txtAddressLable" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:layout_weight="0.25" 
      android:textSize="16sp" 
      android:text="Address : "/> 
     <TextView 
      android:id="@+id/txtAddressValue" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:layout_marginLeft="3dp" 
      android:textSize="16sp" 
      android:text="Flat A, 25/F, Block 2,\nUSA...."/> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp"> 
     <TextView 
      android:id="@+id/txtGenderLable" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:layout_weight="0.25" 
      android:textSize="16sp" 
      android:text="Gender : "/> 
     <TextView 
      android:id="@+id/txtGenderValue" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:layout_marginLeft="3dp" 
      android:textSize="16sp" 
      android:text="Male"/> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp"> 
     <TextView 
      android:id="@+id/txtHobbiesLable" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:layout_weight="0.25" 
      android:textSize="16sp" 
      android:text="Hobbies : "/> 
     <TextView 
      android:id="@+id/txtHobbiesValue" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:layout_marginLeft="3dp" 
      android:textSize="16sp" 
      android:text="Football,Baseball \n......"/> 
    </LinearLayout> 
</LinearLayout> 
+1

@ user782104: Pouvez-vous s'il vous plaît laissez-nous savoir si cela fonctionne? Je pense qu'il devrait, mais je suis curieux de savoir comment il s'est avéré avec des longueurs de texte variables. Je suppose que vous pouvez toujours ajuster les poids pour arriver à des valeurs optimales. – Raghu

+0

sûr, mais est-ce le travail pour tous les appareils dans une résolution différente? par exemple. 0.75 est pour xxhdpi mais 0.5 pour hdpi etc ... – user782104

+0

fonctionne comme prévu mais je crois que le poids doit être changé si la locale est differenet – user782104

2

Fondamentalement, ce que vous voulez, c'est un tableau qui contient des lignes avec deux colonnes. Je devine, vous devriez utiliser le TableLayout pour disposer les twi TextViews.

Bonne chance

Questions connexes