【问题标题】:How to make sure recycler view adapter item takes full height of the screen如何确保回收站视图适配器项目占据整个屏幕高度
【发布时间】:2020-11-04 08:45:21
【问题描述】:

我有一个包含许多项目的回收站视图。

但第一个项目就像一个介绍屏幕,它有一个按钮,用户可以点击该按钮查看更多信息,或者用户可以滚动查看更多数据。

如下所示。

我希望这个按钮位于屏幕底部,在小型设备中,除非用户滚动视图,否则它是不可见的。所以基本上布局占用了更多的高度,所以用户必须滚动才能查看完整的布局。

我正在使用约束布局,并将其连接到视图的底部。

视图中只有2个项目,1个是imageView,它没有任何图像,我只是给它添加了背景色,另一个是TextView。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
 
 
 <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/Black_Color"
        android:scaleType="centerCrop"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
        
        
 <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:gravity="center"
        android:layout_marginBottom="50dp"
        android:textSize="17dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</      

【问题讨论】:

  • 需要更多详细信息。

标签: android android-layout android-recyclerview android-constraintlayout


【解决方案1】:

在小型设备中,除非用户滚动视图,否则它是不可见的 - 听起来您的图像在小型设备中占用了很多屏幕高度,因此用户必须滚动屏幕

如果您想“禁用”您的项目的滚动行为并将所有内容放在屏幕内(没有视图走出屏幕并因此使用户滚动),您可以更改布局,以便您的项目仅占据给定的屏幕高度而不是更多,这样做在小型设备上不会出现这个问题。

类似这样的:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHeight_percent="0.9"
        android:scaleType="fitXY"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/backgrounds/scenic" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="TextView"
        android:textSize="17sp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

诀窍是像这样简单地以百分比形式给出图像高度

  android:layout_height="0dp"
  app:layout_constraintHeight_percent="0.9"

现在您的图像在每个屏幕尺寸上都将占据 90% 的高度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2018-12-22
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多