2016-12-02 2 views
1

У меня есть этот интерфейс:Align TextView ширины в различных LinearLayouts

enter image description here

Использование ниже фрагмента XML. Можно ли выровнять ширину TextViews, чтобы они были одинаковой ширины?

Например, ODOMETER является самым длинным словом в текстовом виде. Как настроить все другие ширины TextView для соответствия ODOMETERS? Таким образом, все они выстраиваются в линию, и интерфейс будет более аккуратным.

<LinearLayout 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:orientation="vertical" 
       tools:context="com.mycompany.myapp.Views.MasterDetails.VehicleFragment"> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="false" 
     android:scrollbars="none"> 

     <LinearLayout 
      android:id="@+id/scrollViewVehicle" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 


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

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="YEAR" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 

       <EditText 
        android:id="@+id/editYear" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:imeOptions="actionDone" 
        android:inputType="number" 
        android:maxLength="4" 
        android:maxLines="1"/> 
      </LinearLayout> 

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

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="MAKE" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 

       <AutoCompleteTextView 
        android:id="@+id/editMake" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:imeOptions="actionDone" 
        android:inputType="text" 
        android:maxLength="30" 
        android:maxLines="1"/> 
      </LinearLayout> 

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

       <TextView 

        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="MODEL" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 


       <AutoCompleteTextView 
        android:id="@+id/editModel" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:imeOptions="actionDone" 
        android:inputType="text" 
        android:maxLength="30" 
        android:maxLines="1"/> 
      </LinearLayout> 

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

       <TextView 

        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="BODY" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 

       <AutoCompleteTextView 
        android:id="@+id/editBody" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:imeOptions="actionDone" 
        android:inputType="text" 
        android:maxLength="30" 
        android:maxLines="1"/> 
      </LinearLayout> 

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

       <TextView 

        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="COLOR" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 

       <AutoCompleteTextView 
        android:id="@+id/editColor" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:imeOptions="actionDone" 
        android:inputType="text" 
        android:maxLength="30" 
        android:maxLines="1"/> 
      </LinearLayout> 

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

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="TAG" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 

       <EditText 
        android:id="@+id/editTag" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:imeOptions="actionDone" 
        android:inputType="text" 
        android:maxLength="30" 
        android:maxLines="1"/> 

      </LinearLayout> 

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

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="STATE" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 

       <AutoCompleteTextView 
        android:id="@+id/editState" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:imeOptions="actionDone" 
        android:inputType="text" 
        android:maxLength="30" 
        android:maxLines="1"/> 

      </LinearLayout> 

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

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="ODOMETER" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 

       <EditText 
        android:id="@+id/editOdometer" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:imeOptions="actionDone" 
        android:inputType="text" 
        android:maxLength="30" 
        android:maxLines="1"/> 
      </LinearLayout> 

      <!-- VIN --> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 

       <TextView 

        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="VIN" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 

       <EditText 
        android:id="@+id/editVIN" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_weight="0.7" 
        android:imeOptions="actionDone" 
        android:inputType="text" 
        android:maxLength="17" 
        android:maxLines="1"/> 

       <Button 
        android:id="@+id/btnScanVIN" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:background="@color/COLOR_TXI_BLUE" 
        android:text="SCAN" 
        android:textColor="@color/COLOR_TXI_WHITE" 
        android:textStyle="bold"/> 
      </LinearLayout> 

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

       <TextView 

        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="OTHER" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 

       <EditText 
        android:id="@+id/editOther" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:imeOptions="actionDone" 
        android:inputType="text" 
        android:maxLength="30" 
        android:maxLines="1"/> 
      </LinearLayout> 


     </LinearLayout> 


    </ScrollView> 


</LinearLayout> 
+0

Рассмотрим с помощью 'GridLayout'. Проверьте это http://stackoverflow.com/questions/10016343/gridlayout-not-gridview-how-to-stretch-all-children-evenly –

ответ

1

Конечно, это возможно. Вы используете горизонтальный макет для каждой пары текстовых полей. Есть следующим образом:

  1. добавить android:weightSum="z" к каждому LinearLayout
  2. добавить android:layout_weight="0.y" каждой этикетки
  3. добавить android:layout_weight="0.x" каждому Text Field

Обратите внимание, что 0.x + 0.y обязательно приравнивает к г. Очевидно, вам нужно установить значения чисел.


EDIT: Этот код:

<LinearLayout 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:orientation="vertical" 
    tools:context="com.mycompany.myapp.Views.MasterDetails.VehicleFragment"> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="false" 
     android:scrollbars="none"> 

     <LinearLayout 
      android:id="@+id/scrollViewVehicle" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 


      <!-- YEAR --> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:weightSum="1" 
       android:orientation="horizontal"> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="YEAR" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 

       <EditText 
        android:id="@+id/editYear" 
        android:layout_width="match_parent" 
        android:layout_weight="0.8" 
        android:layout_height="match_parent" 
        android:imeOptions="actionDone" 
        android:inputType="number" 
        android:maxLength="4" 
        android:maxLines="1"/> 
      </LinearLayout> 

      <!-- MAKE --> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:weightSum="1" 
       android:orientation="horizontal"> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" 
        android:paddingTop="10dp" 
        android:text="MAKE" 
        android:textColor="@color/COLOR_TXI_BLUE" 
        android:textStyle="bold"/> 

       <AutoCompleteTextView 
        android:id="@+id/editMake" 
        android:layout_width="match_parent" 
        android:layout_weight="0.8" 
        android:layout_height="match_parent" 
        android:imeOptions="actionDone" 
        android:inputType="text" 
        android:maxLength="30" 
        android:maxLines="1"/> 
      </LinearLayout> 
     </LinearLayout> 


    </ScrollView> 


</LinearLayout> 

прекрасно работает, и я получаю этот результат enter image description here

+0

Это не работает. Я использовал 0.2 для телевизора и 0.8 для ET. Я добавил 1 к сумме для каждой LL –

+0

установленной ширины в 'math_parent' на обоих ET и ярлыках –

+0

Они уже есть. Это в моем опубликованном коде. –