4

リストビューの行の私のxmlコードは

    <?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:baselineAligned="false"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="2"
        android:gravity="center" >

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/app_icon_17"
            android:contentDescription="@string/empty" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="2"
        android:gravity="center" >

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/btn_delete_new"
            android:focusable="false" />
    </LinearLayout>

</LinearLayout>

ID btn_delete の最後のボタンを右揃えにしたい。そして、設計中の「グラフィカルレイアウト」で私が望んでいたように表示されます。しかし、エミュレーターで実行すると、機能しません。

出力を参照してください:

ここに画像の説明を入力

では、なぜ削除ボタンが右揃えにならないのでしょうか?

4

5 に答える 5

8

相対レイアウトを使用して、これを使用します

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_name"
            android:layout_marginLeft="10dp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" >

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/btn_delete_new"
            android:focusable="false" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_toLeftOf="@+id/rl_btn" >

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/app_icon_17"
            android:contentDescription="@string/empty" />
    </RelativeLayout>

</RelativeLayout>
于 2013-01-07T10:29:55.637 に答える
5

レイアウトを誤解しているようです。埋め込まれたLinearLayoutsものは必要ありません。を使用しますRelativeLayout。例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp" />

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/tv_time"
        android:background="@drawable/app_icon_17"
        android:contentDescription="@string/empty" />

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/btn_delete_new"
        android:focusable="false" />

</RelativeLayout>
于 2013-01-07T10:18:47.353 に答える
1

ご覧のとおり、行レイアウトに重みを割り当てました。

親レイアウトに重みを割り当てる場合は、それに応じて height または with を設定する必要があります0dp。あなたの場合android:layout_width="0dp"

ただし、その重み付けされたレイアウトの子レイアウトがある場合は、そのレイアウト プロパティをfill_parentie: ボタンの android:layout_height="fill_parent"

したがって、親レイアウトを変更せずに既存のコードを使用したい場合は、以下のコードを試すことができます。

<?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:baselineAligned="false"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        >

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:background="@drawable/icon"
            android:contentDescription="@string/app_name" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2" >

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:background="@drawable/icon"
            android:focusable="false" />

    </LinearLayout>

</LinearLayout>
于 2013-01-07T10:36:04.333 に答える
0

これを変える

<LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="2"
        android:gravity="center" >

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/btn_delete_new"
            android:focusable="false" />
    </LinearLayout>

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="right"
            android:background="@drawable/btn_delete_new"
            android:focusable="false" />
    </LinearLayout>

android:layout_gravity="right"あなたはレイアウトに応募していました。代わりに、レイアウト内のボタンに適用する必要があります。そして、そのレイアウトに重み 2 は必要ないと思います。ただし、必要に応じて追加できます。それが役に立てば幸い。

于 2013-01-07T10:15:16.737 に答える
-3
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="right"
        android:gravity="right"
        android:background="@drawable/btn_delete_new"
        android:focusable="false" />
</LinearLayout>
于 2013-01-07T10:12:37.370 に答える