【问题标题】:Custom dialog height to match content自定义对话框高度以匹配内容
【发布时间】:2013-06-24 04:58:01
【问题描述】:

这是我的自定义对话框,有一些文本框和两个按钮。 我在它上面使用了一个RelativeLayout。

我希望对话框大小与内容相匹配,而不是下面的所有空白区域。 我不想使用明确的像素高度(这不是一个好习惯)。

另外,有办法在每个视图之间留出一些空间吗?

这是我的 XML

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

<TextView
    android:id="@+id/txt_desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="8pt"
    android:textStyle="italic" />

<TextView
    android:id="@+id/txt_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"         
    android:layout_marginLeft="10dp"
    android:layout_below="@+id/txt_desc"
    android:textSize="8pt" />

<TextView
    android:id="@+id/txt_place"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"         
    android:layout_marginLeft="10dp"
    android:layout_below="@+id/txt_date"
    android:textSize="8pt" />

<Button
    android:id="@+id/btn_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/txt_place"
    android:layout_toRightOf="@+id/view"
    android:text="@string/btn_close" />

<View
    android:id="@+id/view"
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/btn_edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txt_place"
    android:layout_toLeftOf="@+id/view"
    android:drawableLeft="@android:drawable/ic_menu_edit"
    android:text="@string/btn_edit" />

</RelativeLayout>

【问题讨论】:

  • 使用 MarginTop|MarginLeft|... 作为您的第二个问题。以 dp 为单位的度量

标签: android android-layout android-relativelayout customdialog


【解决方案1】:

第一个问题

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

第二个问题

如果您想将视图与顶部分开 5 dp,请使用 android:marginTop="5dp"。 marginLeft|Right|Bottom 也可用。

<TextView
    android:id="@+id/txt_place"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"         
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp" <-----------------
    android:layout_below="@+id/txt_date"
    android:textSize="8pt" />

顺便说一句,如果我是你,我会嵌套一些布局来整理。您可以有一个通用的相对布局,然后是 2 种线性布局:一种用于 TextView 的水平布局,一种用于 Button 的垂直布局。您可以明确地使用您的编辑器并尝试为您的对话框制作最佳外观。

编辑

试试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000"
    android:orientation="vertical"
    android:paddingBottom="50dp" >

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txt_desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="prova"
                android:textSize="8pt"
                android:textStyle="italic" />

            <TextView
                android:id="@+id/txt_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="prova"
                android:textSize="8pt" />

            <TextView
                android:id="@+id/txt_place"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="prova"
                android:textSize="8pt" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <Button
                android:id="@+id/btn_edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@android:drawable/ic_menu_edit"
                android:text="btn_edit" />

            <Button
                android:id="@+id/btn_close"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="btn_close" />

            <View
                android:id="@+id/view"
                android:layout_width="0dp"
                android:layout_height="1dp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

我已经对文本进行了硬编码。主要思想是在后台使用一个大容器布局,它是透明的 (background=#0000),最重要的是 wrap_content 中包含您的视图的布局。

如果此解决方案不能解决您的问题,我认为您可以直接在代码中编辑高度。尝试联系this question

【讨论】:

  • 第二个已解决,但对于第一个,我尝试将两者都更改为 wrap_content,但似乎没有任何反应:/ 在 wrap_contet 中更改它们会使按钮重叠。现在它看起来就像一个按钮宽的所有对话框。
  • 这可能有点粗糙但是.. 你为什么不在&lt;RelativeLayout 节点中尝试android:paddingBottom="50dp"?我只看到在线性布局上使用它(在这种情况下,尝试更改布局只是为了尝试)
  • 它不起作用。即使使用 paddingBottom,对话框也会保持该高度
  • 你最里面的垂直线性布局是不必要的。一个包含 TextViews 的垂直 LinearLayout 和一个水平 LinearLayout 是重现原始布局所必需的。
【解决方案2】:

将您的相对布局高度设置为wrap_content

例如:

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

    <!-- More XML -->

</RelativeLayout>

还要注意,fill_parent 的宽度和高度已弃用。

您可以分别使用android:layout_marginandroid:padding 添加边距(视图外的额外空间)或填充(视图内的额外空间)。还有一些变体,例如 layout_marginTop,仅适用于视图的特定侧。

【讨论】:

  • 我已经改变了 layout_width 和 layout_height 就像你说的,但没有任何改变:(
  • 这是在 DialogFragment 中使用的吗?
  • 我有 dialog_show XML,然后在我的 java 中使用 Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.dialog_show);和 dialog.show();
  • 您使用RelativeLayout 是否有特殊原因?看起来LinearLayout 可能就足够了。如果您确实切换,您会看到相同的行为吗?
  • 不敢相信它真的那么简单。使用 LinearLayout 有效!我将 RelativeLayout 用于 ToRightOf 属性,但我刚刚发现使用具有垂直和水平的嵌套 LinearLayout 会更好。谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 2014-02-11
  • 1970-01-01
  • 2015-11-11
  • 1970-01-01
相关资源
最近更新 更多