2016-12-02 45 views
1

我有這樣的接口:在不同LinearLayouts對齊的TextView寬度

enter image description here

使用以下片段XML。 是否可以對齊TextViews的寬度,使它們都是相同的寬度?

例如,ODOMETER似乎是textview中最長的單詞。如何調整所有其他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"到每個文本字段

注意0.X + 0.y必須等於ž。顯然你需要設置數字值。


編輯: 此代碼:

<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。我在ET和標籤 –

+0

設置寬度的總和加到'math_parent'的總和上。它在我的發佈代碼中。 –

+0

上將每個LL –