15

私は3つの要素を持つレイアウトを持っており、その向きは「水平」です

レイアウトで各要素が占めるサイズを修正したいと思います。

最初の要素は合計スペースの40%を占め、次に2番目の要素は5%を占め、3番目の要素は残りの55%のスペースを占める必要があります。

私のレイアウトは次のようになります。

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


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100" >

    <TextView
        android:id="@+id/outletname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="40"
        android:text="@string/outlet_name2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:text=": " />

    <TextView
        android:id="@+id/outletnamevalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="55"
        android:text="abcdefttt" />
</LinearLayout>
</LinearLayout>

'TextView'のテキストは動的に変化するため、ビューのコンテンツに関係なく、要素のスペースを一定に保ちたいと思います。

4

2 に答える 2

51

ウェイトを効果的にするには、TextViewの0dpに設定する必要もあります。

android:layout_width="0dp"
于 2012-04-11T09:03:17.010 に答える
0

私はこの式を使用します:

layout_weight=(weightSum-PercentageYouWant*weightSum)/(TotalElements-1)

最初の要素は合計スペースの40%を占め、次に2番目の要素は5%を占め、3番目の要素は残りの55%のスペースを占める必要があります。

1番目の要素:

(100-0.4*100)/(3-1)="30"

2番目の要素:

(100-0.05*100)/(3-1)="47.5"

3番目の要素:

(100-0.55*100)/(3-1)="22.5"

それで40% + 5% + 55% = 100%

layout_weight="30" + layout_weight="47.5" + layout_weight="22.5" = weightSum="100"

それが私がそれを理解する方法です。

ここに例があります(ボタンはグラフィック参照用です)

<?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:weightSum="120" >

    <!-- WeightSum=120 and I will add 4 elements, so TotalElements=4 -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="36"
        android:orientation="vertical" >

        <!-- I want this LinearLayout to be 10%, so (120-10%*120)/(4-1)=36 -->

        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="10%" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="32"
        android:orientation="vertical" >

        <!-- I want this LinearLayout to be 20%, so (120-20%*120)/(4-1)=32 -->

        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="20%" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="28"
        android:orientation="vertical" >

        <!-- I want this LinearLayout to be 30%, so (120-30%*120)/(4-1)=28 -->

        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="30%" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="24"
        android:orientation="vertical" >

        <!-- I want this LinearLayout to be 40%, so (120-40%*120)/(4-1)=24 -->

        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="40%" />
    </LinearLayout>

</LinearLayout>
于 2013-12-26T02:29:56.723 に答える