0

例如,我有一個ListView裏面,每一行的數據是這樣的如何對齊文本android的文本視圖

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

而且上面是它目前如何看待喜歡,我用4文本視圖來表示每個數據

<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> 

問題是我該如何對齊這樣的文本?

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

感謝您的幫助

更新

<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> 

以上是更新的代碼,但問題是,既然是ListView和我發現左側寬度不相等。這是像例如

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

你可以使用8個textviews。我不認爲你可以只使用4個文字瀏覽 – novic3

+0

是和使用重量佈局,但我怎樣才能定義重量?謝謝 – user782104

+0

例如1名爲ABC,ABC先生爲9名?但是如果設備屏幕太小而無法顯示名稱呢? – user782104

回答

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:請告訴我們這是否可行?我認爲它應該,但我很好奇它是如何變化的文本長度。我想,你總是可以調整權重以獲得最佳值。 – Raghu

+0

當然,但是它適用於所有不同分辨率的設備嗎?例如0.75是xxhdpi,但hdpi等是0.5 ... – user782104

+0

按預期工作,但我相信如果語言環境是differenet,則需要更改權重 – user782104

2

基本上你想要的是一個包含兩列的行的表。我猜測,你應該使用TableLayout來佈局twi TextViews。

好運