我有一个 Android TV 应用程序,它在 TextView 下方有一个 RecyclerView。一切看起来都很棒,直到我单击第三个项目,导致 RecyclerView 覆盖 TextView。
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:keepScreenOn="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:clipChildren="false"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="@+id/dateTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginStart="@dimen/homeHorizontalMargin"
android:paddingBottom="20dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_below="@id/dateTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="@dimen/epgRVHorizontalMargin"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="afterDescendants" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
如何阻止 RecyclerView 与 TextView 重叠?
您的
RecyclerView
父布局具有以下属性
android:clipToPadding="false"
android:clipChildren="false"
因此,当您滚动
RecyclerView
时,它将与布局中的其他视图重叠。
您可以将其删除。为什么在这里使用这个属性有什么具体原因要在屏幕上设计其他视图吗?
或者您有
ConstraintLayout
作为根布局,那么为什么您再次添加 RelativeLayout
并将 RecyclerView
放入此布局中。
您可以像下面这样设计您的 XML 布局
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:keepScreenOn="true">
<TextView
android:id="@+id/dateTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/homeHorizontalMargin"
android:paddingBottom="20dp"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@id/dateTV"
android:layout_marginHorizontal="@dimen/epgRVHorizontalMargin"
android:layout_marginTop="20dp"
android:descendantFocusability="afterDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/dateTV" />
</androidx.constraintlayout.widget.ConstraintLayout>