首页 > 解决方案 > FrameLayout 中的嵌套 LinearLayout 与父级不匹配

问题描述

我有以下布局,在 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="wrap_content"> 

<ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img"
        android:layout_gravity="bottom|start"/>

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"/>

</LinearLayout>

嵌套的 LinearLayout 与指定的父级不匹配,但将其内容包装为按钮的大小,我还尝试将匹配父级设置为 FrameLayout 的高度,

这里有什么问题?

标签: androidandroid-framelayout

解决方案


对 FrameLayout 使用 match_parent

<?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"> 

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img"
        android:layout_gravity="bottom|start"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"/>

    </LinearLayout>

推荐阅读