【问题标题】:ListView in a custom dialog pushes buttons off the screen自定义对话框中的 ListView 将按钮推离屏幕
【发布时间】:2012-12-20 07:08:42
【问题描述】:

我正在使用我自己的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>

【问题讨论】:

  • 您找到解决方案了吗?我正在经历同样的事情。
  • @StuartRobertson 不幸的是没有,我最终使用我在问题中提到的 maxHeight 东西在代码中做到了。

标签: android android-layout dialog android-dialog customdialog


【解决方案1】:

而不是框架布局,最好使用线性。它会工作..如果你发布 xml 代码,那么我可以检查..

【讨论】:

  • 使用 FrameLayout 是因为这只是我想要实际放入对话框中的任何内容的容器(即两个示例屏幕截图中的列表和消息)。我尝试使用LinearLayout,但没有任何区别。在我的示例中,列表视图实际上只是一个ListView(在RelativeLayout 内),我将它膨胀并添加到我对话框的onCreate 实现中的FrameLayout。 FWIW,我也会用该布局的内容更新问题......
  • 我已经使用添加到 FrameLayout 的布局更新了问题。如您所见,没有填充或边距。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 2021-02-06
  • 1970-01-01
相关资源
最近更新 更多