0

в следующем макете xml Я хочу создать макет таблицы, в котором размещены некоторые текстовые изображения. результаты ниже размещенного xml-макета - это четыре текстовых поля только у каждого других без интервала. я знаю, что я могу использовать поля на расстоянии друг от друга на расстоянии друг от друга, но я хочу сделать это расстояние между представлениями с помощью гравитационной ориентации макет-гравитация.Как выровнять представления внутри tablelayout

то, что я хочу иметь в качестве результата

odometer:value space space space space space tfu:val 

я хочу, тф, который будет отображаться alwasy в том же положении, независимо от числа разрядов значения одометра. Другими словами, рассмотрим следующий пример, который я не хочу иметь

odometer:1234567       tfu:1234567 
odometer:1234567890       tfu:1234567 

то, что я хочу есть, независимо от количества цифр одометр ФПУ TextView должен быть всегда displaed в позиции Сэма в следующем примере:

odometer:1234567       tfu:1234567 
odometer:1234567890121223434    tfu:1234567 

please tell me how achieve that. 

код:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:fitsSystemWindows="true" 
tools:context=".MainActivity"> 

<TableLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:shrinkColumns="*" 
android:collapseColumns="*" 
android:orientation="vertical"> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/tv_label_odometer" 
     android:text="odometer: "/> 
    <TextView 
     android:id="@+id/tv_odometer" 
     android:text="val"/> 
    <TextView 
     android:id="@+id/tv_label_tfu" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="tfu: "/> 
    <TextView 
     android:id="@+id/tv_tfu"/> 
</TableRow> 
</TableLayout> 


</RelativeLayout> 

ответ

1
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:fitsSystemWindows="true" 
tools:context=".MainActivity"> 

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:shrinkColumns="*" 
    android:collapseColumns="*" 
    android:orientation="vertical"> 

    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:orientation="horizontal"> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 
     <TextView 
      android:id="@+id/tv_label_odometer" 
      android:text="odometer: "/> 
     <TextView 
      android:id="@+id/tv_odometer" 
      android:text="val"/> 
    </LinearLayout> 
     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 
     <TextView 
      android:id="@+id/tv_label_tfu" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:text="tfu: "/> 
     <TextView 
      android:id="@+id/tv_tfu"/> 
     </LinearLayout> 
    </TableRow> 
</TableLayout> 
</RelativeLayout> 

Добавить LinearLayout обернуть два TextView и установить его ширину 0dp и layout_weight в 1.then два LinearLayout будет иметь половину ширины своего родителя, я думаю, что это может решить вашу проблему.

+0

это верно, но я думаю, что у забыл добавить ширину и высоту в TextView в первом LinearLayout – LetsamrIt

+0

Да, я просто скопировать код и добавить 'LinearLayout' обернуть' TextView', вы можете добавить их как тебе нужно. – starkshang

1

Попробуйте вместо

<RelativeLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
        > 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_alignParentLeft="true"/> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_alignParentRight="true"/> 

        </RelativeLayout> 

Это тот, который я использую. Один справа и один слева. Просто повторите его, если хотите больше, и используйте поле, чтобы отрегулировать пространство.

1
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity" > 

    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/tv_label_odometer" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="odometer: " /> 

      <TextView 
       android:id="@+id/tv_odometer" 
       android:layout_width="0sp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:text="1234567" /> 

      <TextView 
       android:id="@+id/tv_label_tfu" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="tfu: " /> 

      <TextView 
       android:id="@+id/tv_tfu" 
       android:layout_width="0sp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="1234567" /> 
     </TableRow> 
    </TableLayout> 

</RelativeLayout> 
Смежные вопросы