2016-04-08 94 views
0

我的RecyclerView中的每個項目在文本下方都有比文本上方更多的可用空間。這裏是我的意思是: Image 1 Image 2Android 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> 

爲什麼會這樣?

kinda解決方案:刪除paddingBottom,將paddingTop放到10dp - 僅適用於固定文本大小。

+0

你在哪裏更新文本視圖中的文本 –

+0

只要將任何其他元素添加到列表行佈局,更改TextView文本大小或將行的高度更改爲某個固定值,您的解決方案就不會工作。我在下面發佈答案,並解釋如何避免這種情況。 –

回答

0

嘗試把paddingbottom只刪除頂部。

+0

這使情況更糟。但現在我刪除了paddingBottom並將paddingTop設置爲10dp,這非常完美。所以,謝謝D – DaveLillo

+1

它發生是因爲第一項有填充底部,然後下一項有填充頂部,所以它使它更大的空間 –

0

這是因爲您使用LinearLayout作爲父容器,其中元素不居中。如果要將它們垂直或水平居中,請使用RelativeLayout作爲子容器的父容器,並使用android:layout_centerHorizontal,android:layout_centerVerticalandroid:layout_centerInParent屬性。

+0

即使我設置android:layout_centerVertical =「true」,它不起作用。仍然錯位... – DaveLillo

+0

@DaveLillo你用LinearLayout替換了LinearLayout嗎? –

+0

@Ales Bernikov是的,我做到了 – DaveLillo