安卓自定义对话框高度

6

我正在学习Android对话框,但我不清楚是什么决定了它们的高度。如果我使用以下XML作为我的对话框布局……

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <Button
     android:id="@+id/AButton"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:layout_alignParentRight="true"
     android:layout_marginLeft="10px"
     android:text="Click Me to Dismiss"
  /> 
  <TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_toLeftOf="@id/AButton"
    android:text="Click this button: " 
    />
   <ImageView
    android:id="@+id/an_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="42px"
    android:src="@drawable/screw50">
  </ImageView>      
  />

我得到了这个对话框...

alt text

但是Developer.Android.com说...

"WRAP_CONTENT表示视图希望尽可能小地包含其内容(加上填充)"

...那么为什么对话框比它需要的高很多呢?

如果我将布局高度替换为

  android:layout_height="200px"

这个对话框应该是适当短小的,但我不想使用明确的像素高度(这不是一个好习惯)。那么我该如何使对话框的大小刚好能容纳其内容呢?

3个回答

7
你将TextView的高度设置为fill_parent。这是否可能与RelativeLayout的高度设置为wrap_content相结合,将其高度推到最大尺寸?如果将TextView的高度更改为wrap_content会发生什么?
另外,Android SDK 表示RelativeLayout的子项不能依赖于布局,如align_parent_bottom。我认为fill_parent是一种依赖关系,因此破坏了布局。

嗨,我的问题与运营有关。我在浏览时看到了这篇文章。实际上,我希望对话框占据尽可能多的空间。这样做的正确方式是什么?即将相对布局的子项设置为填充父项? - Naveed
我相信在那个例子中,你应该将RelativeLayout设置为fill_parent。 - AndrewKS

1

我已经在同一个问题上工作了两天。最终,通过使用对话框样式中的一些属性,它正常工作了。

这是我正在使用的样式:

<style name="SearchFieldSetterDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:gravity">bottom</item>
</style>

关键属性包括:

    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>

首先,我猜需要告诉windowManager在对话框周围放置框架的第一个操作, 而第二个操作是将为对话框标题预留的空间拿出来。

使用这种样式和以下布局,我终于使它正常工作了:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

...

</LinearLayout>

我对这个解决方案的问题是,当android:windowIsFloating属性设置为true并且平台早于3.0时,即使将layout_width设置为fill_parent,我也找不到一种方法使对话框宽度填充屏幕。

希望这能帮助到某些人...对我的糟糕英语表示抱歉。


你在哪里设置对话框的样式属性? - Seraphim's
在构造函数中: http://developer.android.com/reference/android/app/Dialog.html#Dialog(android.content.Context, int) - Mario Lenci

1

嗯,这很奇怪。我使用 <LinearLayout/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical"
                  android:background="@android:color/white">
<TextView android:id="@+id/message"
              android:padding="5dp"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_marginBottom="10dp"/>
    ...
    </LinearLayout>

它很好地包裹了我的内容。


1
是的,我同意LinearLayout没有这个问题。但是我想使用RelativeLayout,因为它可以更好地控制布局。RelativeLayout有什么限制或需要注意的地方吗?(我应该提到我正在使用Android 2.2,在模拟器和实际手机上都出现了这个问题。) - Peter Nelson
为什么不在LinearLayout中使用RelativeLayout?我不知道有什么原因。 - Amir Raminfar
1
为什么不在LinearLayout中使用RelativeLayout呢?我可以尝试这样做,但这似乎是一种hack方法,无意冒犯。肯定有一种正确的方法来解决这个问题。 - Peter Nelson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接