2

私はアンドロイドが初めてで、線形レイアウトで作業しています。画面の 20% を占める画像を配置したいと考えています。ここにコードがありますが、これは機能しません。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/sky" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="2" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/schoolroad" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="8" >
</RelativeLayout>

</LinearLayout>

その結果、左側のレイアウトが画面の 80% を占めます。

コードの何が問題になっていますか?

4

4 に答える 4

9

あなたの向きを指定する必要がありますLinearLayout

また、重みを使用する場合は、幅 (または高さ) を向きに対応する 0dp に設定する必要があります。

したがって、向きが垂直の場合は、重みを使用するときに高さをゼロに設定する必要があります。ウェイトを使用する場合、幅よりも水平の場合はゼロにする必要があります。

これを試して:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/sky" 

    android:orientation="vertical"

    >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="2" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/schoolroad" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="8" >
    </RelativeLayout>

</LinearLayout>
于 2013-06-21T16:45:09.767 に答える
1

を含む子ビューのandroid:layout_width="0dp"代わりに使用します。fill_parentlayout_weight

于 2013-06-21T16:46:21.283 に答える
0

これを試して:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/LinearLayout1"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/sky" >

    <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="2">

        <ImageView
                android:id="@+id/imageView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/schoolroad" />

    </RelativeLayout>

    <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="8">
    </RelativeLayout>

</LinearLayout>
于 2013-06-21T16:46:21.633 に答える