3

私は独自のサブクラスを使用してDialogおり、画面を埋めるのに十分なコンテンツがない場合、コンテンツに合わせて縮小するように構築しようとしています(つまりwrap_content)。次のレイアウトでこれを行うことができます。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_bg">

    <RelativeLayout
        android:id="@+id/title_container"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center_vertical"
        android:background="@drawable/dialog_title_bg">

        . . .

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/button_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/content"
        android:orientation="horizontal">

        . . .

    </LinearLayout>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title_container">
    </FrameLayout>
</RelativeLayout>

(重要ではないと思われるビットを削除しました)。これは、コンテンツが画面いっぱいにならない場合にうまく機能します。

しかし、コンテンツが大きい場合は、ボタンを下から押し出します。

少し調整して、 を変更して、 をその上button_container置きます。次のようにします。layout_alignParentBottom="true"content

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_bg">

    <RelativeLayout
        android:id="@+id/title_container"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center_vertical"
        android:background="@drawable/dialog_title_bg">

        . . .

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/button_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        . . .

    </LinearLayout>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title_container"
        android:layout_above="@+id/button_container">
    </FrameLayout>
</RelativeLayout>

コンテンツが画面に対して大きすぎる場合に機能するようになりました。

ただし、コンテンツが画面いっぱいに表示されない場合でも、ダイアログは次のことを行います。

両方の長所を活かすにはどうすればよいですか? 「maxHeight」を画面の高さからダイアログの境界線とタイトルとボタンの高さを差し引いた値に設定することで、おそらくコードでそれを行うことができますが、それは私には少しハックに思えます。AlertDialog の実装 (この状況を正しく処理しているようです) を調べてみましたが、私には黒魔術のように見えました...

編集:FrameLayout要求に応じて、ダイアログの「コンテンツ」を表すに追加するレイアウトファイルのコンテンツを次に示します。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ListView
        android:id="@+id/report_items"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

</RelativeLayout>
4

1 に答える 1

0

フレーム レイアウトの代わりに、リニアを使用することをお勧めします。それは動作します..xmlコードを投稿すると、確認できます..

于 2012-12-20T07:22:48.870 に答える