【问题标题】:Android custom dialog heightAndroid自定义对话框高度
【发布时间】:2010-12-01 14:57:54
【问题描述】:

我正在学习 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>      
  />

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

但Developer.Android.com 这么说...

"WRAP_CONTENT,这意味着视图希望足够大以包含其内容(加上填充)"

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

如果我用

替换布局高度
  android:layout_height="200px"

它会生成一个适当的简短对话框,但我不想使用明确的像素高度(这不是一个好习惯)。如何使对话框足够大以容纳其内容?

【问题讨论】:

    标签: android dialog


    【解决方案1】:

    您的 TextView 的高度为 fill_parent。这是否有可能与 wrap_content 的 RelativeLayout 的高度相结合将其高度推到最大尺寸?如果将 TextView 高度更改为 wrap_content 会怎样?

    补充一点,the Android SDK 表示 RelativeLayout 的子级不能依赖于 Layout,例如 align_parent_bottom。我假设 fill_parent 是一个依赖项,因此破坏了布局。

    【讨论】:

    • 嗨,我的问题是操作问题。我正在浏览并登陆这篇文章。我实际上希望对话框占用尽可能多的空间。这是正确的做法,即设置一个相对布局的子元素来填充父元素?
    • 我相信你应该将RelativeLayout设置为fill_parent,在那个例子中。
    【解决方案2】:

    嗯,这很奇怪。我用&lt;LinearLayout/&gt;

        <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>
    

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

    【讨论】:

    • 是的,我同意 LinearLayout 没有这个问题。但是我想使用RelativeLayout,因为它给了我对位置的控制。 RelativeLayout 是否有一些限制或陷阱? (我应该提一下,我使用的是 Android 2.2,这个问题发生在仿真和实际手机上。)
    • 为什么不在 LinearLayout 中使用 RelativeLayout?不,我什么都不知道:(
    • "为什么不在 LinearLayout 中使用 RelativeLayout?"我可以尝试一下,但这似乎是一种黑客行为,没有冒犯。必须有办法让它正常工作。
    【解决方案3】:

    过去 2 天我一直在解决同样的问题。最后,它使用我的对话框样式中的一些属性正常工作。

    这是我正在使用的样式:

    <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 在对话框周围放置 Frames 第二个是为对话框标题保留的空间。

    有了这种风格和以下布局,我终于让它工作了:

    <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

    希望这会对某人有所帮助......并为我的英语不好而道歉

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多