2016-11-16 11 views
0

私は、私が持っているリストビューの行レイアウトである次のコードを持っています。ラベルは左に、データは右にしたいです。データは左側で整列する必要がありますが、現時点では曲がって見えます。LinearLayoutアラインメントウェイトパラメータを使用してTextviewを整列する

私はlinearlayoutのtextviewsのウェイトを指定しようとしました。ラベルの重さは1で、データの重さは5です。横幅の5/6を占めるので、データのtextviewは縦に並んでいて、ラベルの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"を指定する必要があります。 このプロパティはアンドロイドに6つの部分でコンテナを分割し、アンドロイド:layout_weightを使用して、1つの要素が1つの要素に収まるようにアンドロイドに指示します。

もう1つのこと:android:layout_weightを設定するときは、対応するwidthまたはheightプロパティに0dpを設定する必要があります。それ以外の場合は、だから、すべてのサブ行の左textviewsが原因layout_width="wrap_content"のアップ揃えされていない例

<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

いいえ、あなたはしません。レイアウトは1 + 5 = 6であることを知っています。OPが求めているものについては – njzk2

+0

、幅は0dpですが正解ですが、決して必須ではありません。 'wrap_content'の設定や固定値が完全に有効なケースがたくさんあります。 – njzk2

+0

@ njzk2 layout_weigthで固定値を使用するにはどうすればよいですか? – firegloves

0

を間違っ

に行くことがあります。彼らはすべてそのコンテンツに応じてスペースを占め、その後はさらに多くのスペースが提供されるweightプロパティによって提供されます。 layout_weightプロパティは、ビューの中で残されている場合、残りのスペースがLinearLayoutのどのように残るかを指示するために使用されます。ここでは2つのことができます。

両方のテキストビューのをすべてのサブローに対して0dpとします。これは、幅の全体が残りのスペースであり、スペースを与える計算が、一致するすべてのサブローに対してのみweightによって行われることを意味します。

または、すべてのテキストビューのwidthを等しく(たとえば40dpのように)定義できます。

関連する問題