0

Например, у меня есть ListView, и для каждой строки, что данные, как этоКак выровнять текст внутри андроида текстового

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 textviews – novic3

+0

yes и используя макет веса, но как я могу определить вес? спасибо – user782104

+0

, например. 1 для имени: и 9 для г-на ABC? но что, если экран устройства слишком мал, чтобы показать имя? – 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, но 0,5 для hdpi и т. Д. – user782104

+0

работает так, как ожидалось, но я считаю, что вес должен быть изменен, если язык отличается – user782104

2

В принципе, вы хотите использовать таблицу, содержащую строки с двумя столбцами. Я предполагаю, что вы должны использовать TableLayout для выкладки twi TextViews.

Good Luck

Смежные вопросы