2

这是我的自定义对话框,有一些文本框和两个按钮。我在它上面使用了一个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>
4

2 回答 2

4

第一个问题

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

第二个问题

如果您想将 View 与 Top 分开 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 中包含您的视图的布局。

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

于 2013-06-26T21:12:44.903 回答
0

将相对布局的高度设置为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_margin您可以使用或分别添加边距(视图外的额外空间)或填充(视图内的额外空间)android:padding。还有一些变体,例如layout_marginTop仅适用于视图的特定侧的变体。

于 2013-06-26T21:12:49.237 回答