首页 > 解决方案 > Android API 21 上缺少包含的布局

问题描述

我有一个应用程序已经在我的设备上运行了很长时间。今天我决定在旧设备上测试它。我有一个旧的 Glaxy S4 Mini,所以我把它加载了,我的布局的整个部分都丢失了。缺少的部分是我制作的可重用布局,并将其包含在此活动中。它所在的空间没有被任何东西占据,它只是空着

这是活动布局文件。当应用程序运行时,里面的所有东西都include完全丢失了。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.eddie.songsPrimirenya.SearchActivityReg">

    <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/search_bar_include"
        layout="@layout/search_bar"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerViewReg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/search_bar_include"
        android:layout_margin="3dp"/>

</RelativeLayout>

这是我之前包含的布局。我认为由于某些兼容性问题它没有显示,但我不知道它是什么。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardSearchReg"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        card_view:cardCornerRadius="5dp"
        card_view:layout_constraintBottom_toBottomOf="parent"
        card_view:layout_constraintEnd_toEndOf="parent"
        card_view:layout_constraintStart_toStartOf="parent"
        card_view:layout_constraintTop_toTopOf="parent"
        card_view:layout_constraintVertical_bias="0.0">

        <RelativeLayout
            android:id="@+id/LayoutAll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:id="@+id/LayoutSearch"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/txtInReg"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:hint="@string/search"
                    android:singleLine="true"
                    android:textColorHint="@color/textHint" />


            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/LayoutSearchModeSelect"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/LayoutSearch"
                android:layout_marginStart="3dp">

                <Switch
                    android:id="@+id/swtSearchType"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="false"
                    android:text="@string/search_by_name"
                    android:textColorHighlight="@color/colorAccent"
                    android:textSize="16sp" />

            </RelativeLayout>

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

这是它应该是什么样子的图像 在此处输入图像描述

这就是它最终的样子 在此处输入图像描述

标签: androidxmlandroid-layout

解决方案


使您的包含布局匹配父级,它应该可以正常工作

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.eddie.songsPrimirenya.SearchActivityReg">

    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/search_bar_include"
        layout="@layout/search_bar"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerViewReg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/search_bar_include"
        android:layout_margin="3dp"/>

</RelativeLayout>

推荐阅读