Android RecyclerView奇怪的项目大小

Android RecyclerView奇怪的项目大小,android,xml,android-recyclerview,Android,Xml,Android Recyclerview,我的RecyclerView中的每个项目在文本下方的可用空间都比文本上方的多。我的意思是: 以下是项目的XML代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent

我的RecyclerView中的每个项目在文本下方的可用空间都比文本上方的多。我的意思是:

以下是项目的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:background="?attr/selectableItemBackground"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:id="@+id/textViewItem" />

</LinearLayout>

为什么会这样


类似的解决方案:删除paddingBottom,将paddingTop设置为10dp-仅适用于固定的文本大小。

尝试将paddingBottom仅删除顶部。

这是因为您使用
LinearLayout
作为父容器,其中元素本身不居中。如果要将它们垂直或水平居中,请使用
RelativeLayout
作为父容器,并使用
android:layout\u centerHorizontal
android:layout\u centerVertical
android:layout\u centerInParent
子视图的属性。

在哪里更新文本视图中的文本?在列表行布局中添加任何其他元素、更改
TextView
文本大小或仅将行高度更改为某个固定值后,解决方案将无法工作。我在下面贴了答案,并解释了如何避免这种情况。这让情况变得更糟。但现在我删除了paddingBottom,并将paddingTop设置为10dp,这很完美。所以,谢谢你:Dit之所以发生,是因为第一个项目的底部有填充物,而下一个项目的顶部有填充物,所以它的空间更大,即使我设置了android:layout\u centerVertical=“true”,它也不起作用。还是放错位置了…@DaveLillo你用RelativeLayout取代了LinearLayout了吗?@Ales Bernikov是的,我有