3

たぶん私はlayout_weightパラメーターを誤解しましたが、なぜこのようなものが機能しないのか理解できません...

<?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="horizontal" android:weightSum="1">

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:text="TextView" 
    android:background="@color/apr_brightyellow"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="TextView" 
    android:background="@color/apr_yellow"/>

</LinearLayout>

LinearLayout内に3つ以上のテキストビューがあります。パーセンテージ幅の列に配置したいと思います。そしてそれはそれが生み出すものです:

layout_weightを0.3/0.2/0.5に設定した場合

Androidのlayout_weightが正しくありません

次に、layout_weightパラメーターを次のように少し変更しました。

<?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="horizontal" android:weightSum="1">

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:text="TextView" 
    android:background="@color/apr_brightyellow"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:text="TextView" 
    android:background="@color/apr_yellow"/>

</LinearLayout>

そして今、私は私が望むようにそれを手に入れます:

layout_weightを0.3/0.2/0.5に設定した場合

Androidのlayout_weightは正しく

それはバグなのか、それともlayout_weightの全体を本当に誤解しているのでしょうか。

4

2 に答える 2

12

使用するandroid:layout_weight場合は設定する必要がありますandroid:layout_width="0dp"(垂直方向の場合- android:layout_height="0dp")。

于 2012-05-11T07:56:55.347 に答える
1

ここに画像の説明を入力してください

これを試して....

        <TextView
            android:background="#F2e"
            android:layout_weight="3"
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
             android:background="#F6e"
            android:layout_weight="2"
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
             android:background="#F45"
            android:layout_weight="5"
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>
于 2012-05-11T07:59:38.540 に答える