【问题标题】:Custom dialog fills all the screen with white background自定义对话框以白色背景填充整个屏幕
【发布时间】:2020-05-19 13:06:42
【问题描述】:

我正在尝试做一个自定义对话框,但每次我运行应用程序时,它都会用白色背景填充屏幕,如下所示: image

查看我的 XML 代码(我已经尝试不使用此 LinearLayout,仅使用 ConstraintLayout,更改背景颜色,但没有任何效果。):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:gravity="center"
android:padding="20dp">

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/shadow"
        android:layout_width="wrap_content"
        android:layout_height="223dp"
        android:layout_marginTop="89pt"
        android:src="@drawable/action_box_shadow_box"
        app:layout_constraintBottom_toTopOf="@+id/box"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <ImageView
        android:id="@+id/triangle"
        android:layout_width="176dp"
        android:layout_height="236dp"
        android:gravity="center"
        android:src="@drawable/action_box_triangle"
        app:layout_constraintBottom_toBottomOf="@+id/box"
        app:layout_constraintEnd_toEndOf="@+id/box"
        app:layout_constraintHorizontal_bias="0.33"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.344" />

    <ImageView
        android:id="@+id/box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/action_box_box"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/action_bar"
        android:layout_width="260dp"
        android:layout_height="55dp"
        android:src="@drawable/action_box_bar"
        app:layout_constraintBottom_toBottomOf="@+id/box"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/box"
        app:layout_constraintVertical_bias="0.811" />

    <TextView
        android:id="@+id/action_text"
        android:layout_width="253dp"
        android:layout_height="102dp"
        app:layout_constraintBottom_toBottomOf="@+id/box"
        app:layout_constraintEnd_toEndOf="@+id/shadow"
        app:layout_constraintStart_toStartOf="@+id/shadow"
        app:layout_constraintTop_toTopOf="@+id/shadow"
        app:layout_constraintVertical_bias="0.286" />

</androidx.constraintlayout.widget.ConstraintLayout>

我认为这是约束的问题,但我不知道如何解决它。

【问题讨论】:

    标签: android xml kotlin layout dialog


    【解决方案1】:

    您正在为视图使用固定高度。固定高度也相当大。您的基本根布局具有wrap_contentfor 高度,因此布局或“对话框”的整体大小将是巨大的。尝试使用wrap_content 作为嵌套视图的高度,看看它的外观。

    对视图使用顶部和底部约束的约束布局,您也可以使用0dp 作为高度。检查一下,看看它如何改变外观。

    【讨论】:

    • 所以我不能将“父”的顶部限制在底部?我尝试使用 wrap_content 作为高度,但没有任何变化。
    猜你喜欢
    • 2016-05-16
    • 2018-05-30
    • 2012-11-20
    • 2023-03-25
    • 1970-01-01
    • 2015-10-23
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多