视图占据全屏高度

时间:2020-08-17 14:27:46

标签: android view

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

仅在特定条件下可见ID为 jobFieldsNoAccess 的视图。 RecyclerView jobFieldList 可以包含一个或多个项目。

我希望视图的整个高度必须等于回收者视图的高度,这就是为什么它是match_parent的原因。但是...

问题是:

即使RecyclerView jobFieldList 包含一项高度很小的项目。但是视图 jobFieldsNoAccess (如果可见)将占据整个屏幕高度。

enter image description here

问题并非在所有设备上都存在。仅在装有Android 10的某些设备上会出现此问题。

为什么视图 jobFieldsNoAccess 占据整个高度?我该如何解决?

2 个答案:

答案 0 :(得分:0)

为什么视图jobFieldsNoAccess占据整个高度?

因为您使用android:layout_height="match_parent"告诉了我们

我该如何解决?

通过固定显式高度或使用wrap_content代替match_parent

答案 1 :(得分:0)

通过使用RelativeLayout而不是FrameLayout解决了该问题。 将View: 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>