FrameLayout的高度不匹配父容器

5

我在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>

很抱歉,布局内容较多,将其原样放在此处。

请注意具有id progress 的底部FrameLayout。尽管它具有android:layout_height="match_parent",但在设备上看起来像是“wrap_content” - 仅具有与内部ProgressBar匹配的高度。 虽然在Android Studio设计师中显示得非常完美,占据了整个视图。

怎么回事?

以下是AS设计师中布局的样子

AS designer

和在设备上的样子(尝试过模拟器和真实设备,效果相同)

Device


1
你能同时发布一张图片和期望的输出结果吗? - Charuක
1
为您的子帧布局添加重力。 - Charuක
@Alexey,请检查我的答案。 - Charuක
@Alexey 我的回答肯定会有帮助! - Arnis Shaykh
4个回答

4
尝试将根FrameLayout更改为RelativeLayout。

0

我曾经遇到过同样的问题并成功解决了。似乎没有人注意到,但你的问题源头是一个GridLayout。正如您在蓝图中所看到的,FrameLayout没有问题,因为它确实与父级匹配。您GridLayout中的项目具有固定大小,这是错误的。您需要为GridLayout中的每个项目设置权重,以便项目可以拉伸以适应任何屏幕分辨率。但是,GridLayout中的权重需要API 21。因此,我建议您使用嵌套的LinearLayout,并将项目的权重设置为1。


不行,我已经尝试使用LinearLayout而不是GridLayout - 问题依旧 :( - Alexey
你设置项目的宽度和高度是固定的还是使用权重? - Arnis Shaykh
我想表达的是,你不应该使用dimen文件夹中的尺寸。 - Arnis Shaykh

0
我在项目中遇到的问题是,我有两个具有相同ID的Frame Layouts。 解决方案是为每个Frame Layout分配唯一的ID。
希望这能帮助未来的读者。

0

你有两个FrameLayout。一个是@id progress,另一个是LinearLayout

首先快速查看一下developer.android.com

FrameLayout旨在阻止屏幕上的某个区域显示单个项目。通常,应该使用FrameLayout来容纳单个子视图,因为如果不重叠,难以以可扩展到不同屏幕尺寸的方式组织子视图。

那么对于你的父级FrameLayout,你有多少个子元素?(2)

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

你的两个子元素都有重力吗?没有,对于@id progress而言没有。

子视图按堆栈顺序绘制,最近添加的子视图位于顶部。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>

输出(我为线性布局添加了一张图片,你在子布局中缺少了gravity):

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接