簡體   English   中英

為什么Lollipop上的CardViews之間沒有空間?

[英]Why there is no space between CardViews on Lollipop?

我嘗試使用CardView ,它在5.0以下工作得很好,但在棒棒糖上看起來很奇怪。

在此輸入圖像描述

在此輸入圖像描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<android.support.v7.widget.CardView android:layout_width="match_parent"
    android:layout_height="200dp">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="card1"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView android:layout_width="match_parent"
    android:layout_height="200dp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="card2"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</android.support.v7.widget.CardView>
</LinearLayout>

當我使用RecyclerView時遇到同樣的問題,如果它在Lollipop上運行,我是否應該添加一些東西?

CardView上設置:

app:cardUseCompatPadding="true"

來自文檔:

在API v21 +中添加填充以與先前版本具有相同的度量。

在cardview內使用以下兩個標簽:

app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"

第一張圖片是卡片視圖的預期行為。 當卡片有高度時,陰影落在底層。 在前棒棒糖裝置中,通過添加填充來進行提升。 所以前棒棒糖設備將在卡片視圖周圍有一個填充物。

在L之前,CardView會在其內容中添加填充並為該區域繪制陰影。 此填充量等於兩側的maxCardElevation +(1 - cos45)* cornerRadius,頂部和底部的maxCardElevation * 1.5 +(1 - cos45)* cornerRadius。

你必須在你的Cardview添加app:cardUseCompatPadding="true" 但只是添加它可能會給你一個錯誤。 要避免該錯誤,您還必須將xmlns:app="http://schemas.android.com/apk/res-auto"到您的CardView

例如,

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:cardUseCompatPadding="true">

    // Other views here

</android.support.v7.widget.CardView>

有些人會添加card_view:cardUseCompatPadding="true"xmlns:card_view="http://schemas.android.com/apk/res-auto"而不是上面提到的那些。 兩種方式都是正確的。

如果您想了解有關XML(Android) 應用的更多信息,請通過以下答案

雖然以前的答案將解決問題,但他們沒有解釋每個屬性的作用。 所以更有幫助回答尋求者,

cardPreventCornerOverlap屬性在v20和之前向cardPreventCornerOverlap添加填充以防止卡內容和圓角之間的交叉。

cardUseCompatPadding屬性在API v21 +中添加填充,以便與先前版本具有相同的度量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM