且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android自定义对话框高度

更新时间:2023-01-23 21:11:41

您的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.