简体   繁体   English

视图正在占用全屏高度

[英]View is taking full screen height

I have the following code in the android XML layout.我在 android XML 布局中有以下代码。

<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. id 为jobFieldsNoAccess的视图仅在特定条件下可见。 The RecyclerView jobFieldList can have one or more items. RecyclerView jobFieldList可以有一个或多个项目。

I want the view must take the whole height equal to the height of the recycler view, that's why it is match_parent.我希望视图的整个高度必须等于回收器视图的高度,这就是它是 match_parent 的原因。 But...但...

The issue is:问题是:

Even the RecyclerView jobFieldList has one item which is taking a small amount of height.甚至 RecyclerView jobFieldList也有一个占用少量高度的项目。 But the view jobFieldsNoAccess , if visible, takes the whole screen height.但是视图jobFieldsNoAccess (如果可见)占据整个屏幕高度。

在此处输入图像描述

The issue is not on all devices.问题不在所有设备上。 Only on some devices with Android 10 has that issue.仅在某些具有 Android 10 的设备上存在该问题。

Why the view jobFieldsNoAccess is taking the whole height?为什么视图jobFieldsNoAccess占据了整个高度? How can I fix that?我该如何解决?

Why the view jobFieldsNoAccess is taking the whole height?为什么视图 jobFieldsNoAccess 占据了整个高度?

Because you tell it to with android:layout_height="match_parent"因为你告诉它android:layout_height="match_parent"

How can I fix that?我该如何解决?

By secifying a explicit height or use wrap_content instead of match_parent .通过指定明确的高度或使用wrap_content而不是match_parent

The issue is resolved by using the RelativeLayout instead of FrameLayout .通过使用RelativeLayout而不是FrameLayout解决了该问题。 Aligned the top and bottom of View: jobFieldsNoAccess with RecyclerView: jobFieldListView: jobFieldsNoAccess的顶部和底部与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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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