0

Androidのlayout_weight属性を効果的に使用する方法がわかりません。3 つの textView を持つカスタム listView を作成しようとしています。プライマリ textView は左上にあり、次に右上にテキスト ビューがあり、次にプライマリ textView の下にある 3 番目のビューがあります。プライマリと右上の 2 つを水平軸に配置したいのですが、プライマリ textView が幅の約 70% を占めるようにし、他の textView が残りの 30% を占めるようにします。プライマリと右の​​ textView にどのような重みを割り当てても、プライマリの textView は常に大きくなり、右のデータ textView があまりにも薄くなります。

ここに画像の説明を入力

私のコード:

<LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:padding="5dip"
              android:id="@+id/Llayout">
<!-- Title-->
<TextView
        android:id="@+id/primaryTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading some tadglkj dgjg sadlgkj dgksgj sgj sdgk"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="18dip"
        android:textStyle="bold"
        android:paddingTop="0dp"
        android:background="#888"
        android:layout_weight="4"/>

<!-- Right Data -->
<TextView
        android:id="@+id/rightData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:layout_marginRight="5dip"
        android:textSize="12dip"
        android:textColor="#B53021"
        android:textStyle="bold"
        android:background="#bada55"
        android:text="1.3 mi"/>
</LinearLayout>

<!-- Secondary title -->
<TextView
        android:id="@+id/secondaryTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#343434"
        android:textSize="12dip"
        android:layout_marginTop="1dip"
        android:text="hello this is some other data"/>

4

4 に答える 4

0

両方の TextView で android:layout_width="wrap_content" を android:layout_width="0dp" に変更するだけです。

android:layout_width="0dp"

android:weightSum は必要ありません。

于 2013-05-14T16:20:28.657 に答える
0

70% にしlayout_weight="30"たい項目には を、30% にしたい項目には を設定しlayout_weight="70"ます。これで問題が解決するはずです。

于 2013-05-14T14:39:22.483 に答える
0

私はいくつかのことをする必要がありました。

  1. TextViews を格納した LinearLayout に weightSum を割り当てます
  2. 次に、TextView の layout_width を「wrap_content」ではなく「0dp」に変更する必要がありました。

    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:id="@+id/Llayout"
                  android:weightSum="10">
    
        <!-- Title-->
        <TextView
                android:id="@+id/primaryTitle"
                android:layout_height="wrap_content"
                android:text="Loading..."
                android:textColor="#040404"
                android:typeface="sans"
                android:textSize="18dip"
                android:textStyle="bold"
                android:paddingTop="0dp"
                android:layout_width="0dp"
                android:layout_weight="8"/>
    
        <!-- Right Data -->
        <TextView
                android:id="@+id/rightData"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:gravity="right"
                android:layout_marginRight="5dip"
                android:textSize="12dip"
                android:textColor="#B53021"
                android:textStyle="bold"
                android:text="..."/>
    </LinearLayout>
    
    <!-- Secondary title -->
    <TextView
            android:id="@+id/secondaryTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#343434"
            android:textSize="12dip"
            android:layout_marginTop="1dip"
            android:text="..."/>
    

于 2013-05-14T14:56:01.720 に答える
0

コンテナに重量の合計を定義する必要があります。あなたの場合、これは次のようになります(必要な関連属性のみを含む):

<LinearLayout 
      android:weightSum="1">
<!-- Title-->
<TextView
     android:layout_weight=".7"/>

<!-- Right Data -->
<TextView
       android:layout_weight=".3"/>
</LinearLayout>
于 2013-05-14T14:42:30.557 に答える