Android自定义对话框高度 [英] Android custom dialog height

查看:179
本文介绍了Android自定义对话框高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Android对话框,我对什么决定了他们的身高感到困惑。如果我将此XML用于我的对话框布局。 。 。

 <?xml version =1.0encoding =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 =点击我来关闭
/>
< TextView android:id =@ + id / text
android:layout_width =wrap_content
android:layout_height =fill_parent
android:layout_toLeftOf =@ id / AButton
android:text =点击此按钮:
/>
< 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

它使一个正确的短对话框,但我不想使用显式像素高度(这不是很好的做法)。如何使对话框的内容足够大?

解决方案

您的TextView的高度为fill_parent。是否可能这样,结合RelativeLayout到wrap_content的高度将其高度推到最大尺寸?如果将TextView高度更改为wrap_content,会发生什么?



要添加 Android SDK 表示,RelativeLayout的子项不能对Layout进行依赖,例如align_parent_bottom。我假设fill_parent是一个依赖关系,因此打破了布局。


I'm learning about Android dialogs and I'm confused about what determines their height. If I use this XML for my dialog layout . . .

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

I get this dialog . . .

But Developer.Android.com says that...

"WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding)"

... so why is the dialog so much higher than it needs to be?

If I replace the layout height with

  android:layout_height="200px"

it makes a properly short dialog but I don't want to use explicit pixel heights (it's not good practice). How do I make the dialog be just big enough for its content?

解决方案

You have the height of your TextView to fill_parent. Is it possible that this, combined with the height of the RelativeLayout to wrap_content is pushing its height to the maximum size? What happens if you change the TextView height to wrap_content?

To add, the Android SDK says that children of a RelativeLayout can not have a dependency on the Layout, such as align_parent_bottom. I assume that fill_parent is a dependency and thus is breaking the layout.

这篇关于Android自定义对话框高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆