首页 文章

FrameLayout高度与父级不匹配

提问于
浏览
2

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

4 回答

  • 2

    尝试将根FrameLayout更改为RelativeLayout

  • 0

    你有两个FrameLayouts . @id进展1和 LinearLayout 一个 .

    首先快速浏览developer.android.com

    FrameLayout旨在阻挡屏幕上的区域以显示单个项目 . 通常,FrameLayout应该用于保存单个子视图,因为很难以可扩展到不同屏幕大小而儿童不会相互重叠的方式组织子视图 .

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

    但是,您可以使用android:layout_gravity属性将多个子项添加到FrameLayout并通过为每个子项分配重力来控制它们在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>
    

    Output (我在您的子框架布局中添加了缺少 gravity 的线性布局图像):

    enter image description here

  • 0

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

  • 0

    我在我的项目中遇到的问题是,我有两个具有相同Id的Frame布局 . 解决方案是为帧布局分配唯一ID .

    希望这有助于未来的读者 .

相关问题