FrameLayout 高度与父级不匹配

Ale*_*xey 5 android android-framelayout

我在 AlertDialog 中有以下布局作为自定义视图。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:columnCount="3">

            <TextView
                android:id="@+id/code1"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:gravity="center"
                android:text="\u2022"
                android:textSize="@dimen/match_code_digit_size"
                tools:ignore="HardcodedText" />

            <TextView
                android:id="@+id/code2"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:layout_marginBottom="20dp"
                android:gravity="center"
                android:text="\u2022"
                android:textSize="@dimen/match_code_digit_size"
                tools:ignore="HardcodedText" />

            <TextView
                android:id="@+id/code3"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:gravity="center"
                android:text="\u2022"
                android:textSize="@dimen/match_code_digit_size"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/k1"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:text="1"
                android:textSize="@dimen/match_code_button_text"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/k2"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:text="2"
                android:textSize="@dimen/match_code_button_text"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/k3"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:text="3"
                android:textSize="@dimen/match_code_button_text"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/k4"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:text="4"
                android:textSize="@dimen/match_code_button_text"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/k5"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:text="5"
                android:textSize="@dimen/match_code_button_text"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/k6"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:text="6"
                android:textSize="@dimen/match_code_button_text"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/k7"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:text="7"
                android:textSize="@dimen/match_code_button_text"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/k8"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:text="8"
                android:textSize="@dimen/match_code_button_text"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/k9"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:text="9"
                android:textSize="@dimen/match_code_button_text"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/k0"
                android:layout_width="@dimen/match_code_button_size"
                android:layout_height="@dimen/match_code_button_size"
                android:layout_column="1"
                android:text="0"
                android:textSize="@dimen/match_code_button_text"
                tools:ignore="HardcodedText" />

        </GridLayout>

        <TextView
            android:id="@+id/error"
            style="@style/ErrorText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/match_error"
            android:visibility="invisible" />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#80ffffff"
        android:visibility="visible">

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

    </FrameLayout>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

对不起,大量的布局,把它“按原样”放在这里。

注意底部带有 id progress 的FrameLayout 。尽管它有android:layout_height="match_parent",但在设备上它看起来像 "wrap_content" - 高度只匹配内部 ProgressBar。虽然在 Android Studio 设计器中显示完美,占据了整个视图。

怎么了?

这是布局在 AS 设计器中的外观

AS设计师

并在设备上(模拟器和真机都试过,效果一样)

设备

Cha*_*ruක 0

您有两个 FrameLayout。@id进展一又一LinearLayout

首先快速浏览一下developer.android.com

FrameLayout 旨在遮挡屏幕上的一个区域以显示单个项目。一般来说,FrameLayout 应该用于保存单个子视图,因为在子视图不相互重叠的情况下,以可缩放到不同屏幕尺寸的方式组织子视图可能很困难。

那么对于你的父母来说,FrameLayout你有几个孩子?(2)

但是,您可以向 FrameLayout 添加多个子项,并使用 android:layout_gravity 属性为每个子项分配重力来控制它们在 FrameLayout 中的位置。

你的两个孩子有重力吗?不,不是为了@id 的进展

子视图在堆栈中绘制,最近添加的子视图位于顶部。FrameLayout 的大小是其最大子级的大小(加上填充),无论是否可见(如果 FrameLayout 的父级允许)。

因此,首先添加线性布局,然后添加 FrameLayout,这样.. 子元素FrameLayout 应该位于顶部,事实确实如此。这就是你的问题吧?

即使你的布局很长也可以缩短到这个

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_gravity="center"
        android:background="#121"
        android:orientation="vertical">
    </LinearLayout>

    <FrameLayout
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="#567"
        android:visibility="visible">

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

    </FrameLayout>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

输出(我添加了线性布局的图像,您的框架布局中缺少重力):

在此输入图像描述