2016-11-16 85 views
0

我有以下的代码,这是我有一个列表视图的行布局。我希望标签位于左侧,数据位于右侧。数据应该在左侧对齐,但目前看起来是歪斜的。LinearLayout对齐Textviews使用重量参数

我曾尝试在linearlayout中为文字指定权重。该标签的权重为1,数据的权重为5.我预计数据文字视图会垂直排列,因为它们应该占据水平宽度的5/6,只剩下标签的1/6。

为什么数据TextViews不是同样占用了屏幕宽度的一样多吗?

在开发人员选项我检查了展会的布局界限,它证明了数据textviews不垂直对齐。

<?xml version="1.0" encoding="utf-8"?> 



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/white" 

    android:paddingTop="10dp" 
    android:paddingBottom="10dp"> 



    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

      <TextView 
        android:id="@+id/carerinfotypelabel" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Type: " 
        android:layout_weight="1" 
        android:textColor="@color/cf_blue" 

         /> 

       <TextView 
        android:id="@+id/carerinfotype" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 

        android:layout_weight="5" 

         /> 

    </LinearLayout> 


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

       <TextView 
        android:id="@+id/carerinfosurnamelabel" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Name: " 
        android:layout_weight="1" 
        android:textColor="@color/cf_blue" 

         /> 

       <TextView 
        android:id="@+id/carerinfosurname" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="5" 
        android:layout_below="@id/carerinfotypelabel" 
        /> 


     </LinearLayout> 



    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

        <TextView 
         android:id="@+id/carerinfoaddresslabel" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Address: " 
         android:layout_weight="1" 
         android:textColor="@color/cf_blue" 
          /> 

        <TextView 
         android:id="@+id/carerinfoaddress" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 

         android:layout_weight="5" /> 


     </LinearLayout> 



    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

        <TextView 
         android:id="@+id/carerinfopostcodelabel" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Postcode: " 
         android:layout_weight="1" 
         android:textColor="@color/cf_blue" 
          /> 

        <TextView 
         android:id="@+id/carerinfopostcode" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="5" /> 

     </LinearLayout> 




    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 


        <TextView 
         android:id="@+id/carerinfomobilelabel" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Mobile: " 
         android:layout_weight="1" 
         android:textColor="@color/cf_blue" 
          /> 

        <TextView 
         android:id="@+id/carerinfomobile" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="5" 

          /> 


     </LinearLayout> 


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 


     <TextView 
      android:id="@+id/carerinfotelhomelabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Home Tel: " 
      android:layout_weight="1" 
      android:textColor="@color/cf_blue" 
       /> 

     <TextView 
      android:id="@+id/carerinfotelhome" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="5" 

       /> 

     </LinearLayout> 



    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 


       <TextView 
        android:id="@+id/carerinfotelno1label" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Tel 1: " 
        android:layout_weight="1" 
        android:textColor="@color/cf_blue" 
        /> 


       <TextView 
        android:id="@+id/carerinfotelno1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 

        android:layout_weight="5" /> 


     </LinearLayout> 



    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

       <TextView 
        android:id="@+id/carerinfotel2label" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Tel 2: " 
        android:layout_weight="1" 
        android:textColor="@color/cf_blue" 
        /> 

       <TextView 
        android:id="@+id/carerinfotel2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="5" 
        /> 

     </LinearLayout> 


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 


       <TextView 
        android:id="@+id/carerinfotel3label" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Tel 3: " 
        android:layout_weight="1" 
        android:textColor="@color/cf_blue"/> 

       <TextView 
        android:id="@+id/carerinfotel3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="5"/> 


     </LinearLayout> 




    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 


        <TextView 
         android:id="@+id/carerinfotel4label" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Tel 4: " 
         android:layout_weight="1" 
         android:textColor="@color/cf_blue"/> 




        <TextView 
         android:id="@+id/carerinfotel4" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="5" /> 


     </LinearLayout> 




    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

        <TextView 
     android:id="@+id/carerinforelationshiplabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Relationship: " 
         android:layout_weight="1" 
     android:textColor="@color/cf_blue" 
     /> 


     <TextView 
      android:id="@+id/carerinforelationship" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:layout_weight="5" 
      /> 

     </LinearLayout> 




    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 


        <TextView 
     android:id="@+id/carerinfodoblabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="DOB: " 
         android:layout_weight="1" 
     android:textColor="@color/cf_blue" 
     /> 


     <TextView 
      android:id="@+id/carerinfodob" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:layout_weight="5" 
      /> 



     </LinearLayout> 



    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 


          <TextView 
          android:id="@+id/carerinfopinlabel" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="PIN: " 
           android:layout_weight="1" 
          android:textColor="@color/cf_blue" 
          /> 


          <TextView 
          android:id="@+id/carerinfopin" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 

           android:layout_weight="5" 
          /> 


    </LinearLayout> 



</LinearLayout> 
+0

因为'wrap_content' – njzk2

回答

-1

您需要在所有水平布局中指定android:weightSum =“6”。 此属性告诉android将单独的容器分成六部分,然后用android:layout_weight告诉android一个元素必须适合多少个部分。

另一件事:当你设置的android:layout_weight必须设置0dp相应宽度或高度属性。否则,可能会出问题

所以一个例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="6"> 


     <TextView 
      android:id="@+id/carerinfodoblabel" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="DOB: " 
      android:layout_weight="1" 
      android:textColor="@color/cf_blue" /> 


     <TextView 
      android:id="@+id/carerinfodob" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="5" /> 

    </LinearLayout> 
+0

不,你没有。布局知道OP要求的是1 + 5 = 6. – njzk2

+0

,“0dp”是宽度是正确的答案,但它绝不是强制性的。有很多情况下,设置'wrap_content'或者一个固定值是完全有效的。 – njzk2

+0

@ njzk2固定值如何与layout_weigth一起使用? – firegloves

0

每个子行的左textviews不对齐,因为layout_width="wrap_content"起来。他们都根据其内容首先占用空间,然后由weight提供更多的空间,这些空间也会有所不同。 layout_weight属性用于指示剩余空间如何留在LinearLayout的视图中使用。你可以在这里做两件事。

要么使双方textviews为0dp为每个子行的layout_width。这意味着整个宽度是剩余空间,并且仅在每个子行将保持一致的情况下通过weight完成给予空间的计算。

或者你也可以手动定义为相等(对于如40dp)每TextView中的width