简体   繁体   中英

View is taking full screen height

I have the following code in the android XML layout.

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/marginSmall"
    android:background="@color/white"
    app:cardCornerRadius="@dimen/item_margin">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/jobFieldList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/margin"
            app:fieldEditable="@{true}"
            app:fieldsMap="@{viewModel.job.jobFields}"
            app:jobFields="@{jobFields}" />

        <View
            android:id="@+id/jobFieldsNoAccess"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0.75"
            android:background="@color/white"
            android:visibility="@{viewModel.attributes.referral.usersReferred >= 2 ? View.GONE : View.VISIBLE}" />

    </FrameLayout>

</androidx.cardview.widget.CardView>

The view with id jobFieldsNoAccess is visible only based on a certain condition. The RecyclerView jobFieldList can have one or more items.

I want the view must take the whole height equal to the height of the recycler view, that's why it is match_parent. But...

The issue is:

Even the RecyclerView jobFieldList has one item which is taking a small amount of height. But the view jobFieldsNoAccess , if visible, takes the whole screen height.

在此处输入图像描述

The issue is not on all devices. Only on some devices with Android 10 has that issue.

Why the view jobFieldsNoAccess is taking the whole height? How can I fix that?

Why the view jobFieldsNoAccess is taking the whole height?

Because you tell it to with android:layout_height="match_parent"

How can I fix that?

By secifying a explicit height or use wrap_content instead of match_parent .

The issue is resolved by using the RelativeLayout instead of FrameLayout . Aligned the top and bottom of View: jobFieldsNoAccess with RecyclerView: jobFieldList

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/marginSmall"
    android:background="@color/white"
    app:cardCornerRadius="@dimen/item_margin">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/jobFieldList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/margin"
            app:fieldEditable="@{true}"
            app:fieldsMap="@{viewModel.job.jobFields}"
            app:jobFields="@{jobFields}" />

        <View
            android:id="@+id/jobFieldsNoAccess"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/jobFieldList"
            android:layout_alignBottom="@+id/jobFieldList"
            android:alpha="0.75"
            android:background="@color/white"
            android:visibility="@{viewModel.attributes.referral.usersReferred >= 2 ? View.GONE : View.VISIBLE}" />

    </RelativeLayout>

</androidx.cardview.widget.CardView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM