【问题标题】:Android DialogFragment layout, Odd extra whitespaceAndroid DialogFragment 布局,奇怪的额外空白
【发布时间】:2013-12-01 03:21:19
【问题描述】:

所以在为一个简单的“让用户选择”弹出窗口制作了dialogfragment 之后,我加载它并在布局中看到一些奇怪的额外空白,我不确定是什么原因造成的,我一直在绞尽脑汁尝试弄明白。我稍微修改了 xml 以更好地显示行为,唯一的区别是我将 3 个垂直部分的宽度从“match_parent”更改为“wrap_content”,因此我可以使用边框来显示空白的实际大小.这是显示问题的图片:

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/pictureSourceMainTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tvborder"
        android:gravity="center"
        android:text="Where would you like to choose\nyour image from?"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:id="@+id/pictureSourceIVLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tvborder"
        android:weightSum="3" >

        <ImageView
            android:id="@+id/pictureSourceCancelIV"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/pictureSourceGalleryIV"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/pictureSourceCameraIV"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/pictureSourceTVLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:weightSum="3" >

        <TextView
            android:id="@+id/pictureSourceCancelTV"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/tvborder"
            android:gravity="center"
            android:text="Cancel" />

        <TextView
            android:id="@+id/pictureSourceGalleryTV"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/tvborder"
            android:gravity="center"
            android:text="Gallery" />

        <TextView
            android:id="@+id/pictureSourceCameraTV"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/tvborder"
            android:gravity="center"
            android:text="Camera" />
    </LinearLayout>

</LinearLayout>

所以我的问题是:我能做些什么来删除这个多余的空格?我的 xml 布局是否缺少某些内容?我可以每次手动设置宽度的大小,但我更希望窗口本身实际上是“wrap_content”

编辑:原来调整大小是由标题引起的,有没有办法将标题缩小到合适的大小?

【问题讨论】:

  • 试着从 xml 编辑器的图形布局选项卡中查看多余的空间是什么?
  • 您的所有Views(包括根LinearLayout)在实际layout 中都有match_parent 用于您的width
  • @user2511882 没有什么可看的,在 xml 编辑器中也没有什么
  • @codeMagic:我从“match_parent”更改为“wrap_content”以演示空白的三个对象是:“pictureSourceMainTextView、pictureSourceIVLayout 和 pictureSourceTVLayout”,当恢复为“match_parent”时,它会填充额外的空间,但我不希望这样,我希望它包装到内容的大小
  • 那么你想要什么地方的空白?此外,在您的第二个孩子LinearLayout 中,您应该拥有android:layout_height="0dp",因为您使用的是weight

标签: android layout dialogfragment


【解决方案1】:

罪魁祸首是标题。设置:

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

在我的对话框片段中的oncreateview 中修复了所有大小问题,我只是在顶部添加了一个新的imageview 并在photoshop 中设计了我自己的标题。

【讨论】:

    【解决方案2】:

    android:layout_widthTextView 更改为"fill_parent"。 也可以将android:gravity="center"加到外层LinearLayout

    【讨论】: