【问题标题】:Why there is no space between CardViews on Lollipop?为什么 Lollipop 上的 CardView 之间没有空格?
【发布时间】:2014-11-20 09:11:21
【问题描述】:

我尝试使用CardView,它在 5.0 以下运行良好,但在 Lollipop 上看起来很奇怪。

<?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 上运行,我是否需要添加一些东西?

【问题讨论】:

  • 在卡片之间添加LinearLayout

标签: android android-cardview


【解决方案1】:

将此设置在CardView:

app:cardUseCompatPadding="true"

来自文档:

在 API v21+ 中添加填充,以使用相同的测量值 以前的版本。

【讨论】:

  • 我改用了“app”命名空间。 “card_view”还在使用吗?
  • @wsgeorge 来自 Android 文档,但我已将其编辑为更友好的应用命名空间。
  • 看起来更好,但如何删除分隔线?
  • @nonews 你可以试试android:divider="@null"
【解决方案2】:

在您的卡片视图中使用下面的这两个标签:

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

【讨论】:

    【解决方案3】:

    第一张图片是卡片视图的预期行为。当卡片有高度时,阴影落在底层。在棒棒糖之前的设备中,通过添加填充来实现高程。所以棒棒糖之前的设备将在卡片视图周围有一个填充。

    在 L 之前,CardView 为其内容添加填充并绘制阴影 那个区域。此填充量等于 maxCardElevation + (1 - cos45) * 边上的cornerRadius 和 maxCardElevation * 1.5 + (1 - cos45) * 顶部和底部的角半径。

    【讨论】:

    • 所以我们在编辑器中看到的cardview的大小不是我们在棒棒糖下面可以使用的真实大小,它的一部分是用来添加填充的,棒棒糖是布局中的大小和真实的一样,好像加个padding更好看,但是怎么分开呢?
    • 您在编辑器中看到的就是您实际得到的。使所有屏幕中的卡片之间的边距相同。如果 API 杠杆为 21 或更高,请设置边距
    • 这个答案是正确的。除此之外,如果您想让 UI 保持一致而不为 values-21 添加单独的边距,您可以将 compat padding 设置为 true,这样它也会在 L 中添加相同的填充。 developer.android.com/reference/android/support/v7/widget/…
    • @billgates 添加边距后,卡片视图在棒棒糖和棒棒糖前看起来都不错,感谢您的回答!
    • 是否可以删除棒棒糖前设备上的填充?
    【解决方案4】:

    您必须将app:cardUseCompatPadding="true" 添加到您的Cardview。但只是添加它可能会给你一个错误。为避免该错误,您还必须将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) 中的 app 的信息,请通过此answer

    虽然以前的答案会解决问题,但他们没有解释每个属性的作用。因此,为了更有助于回答寻求者,

    cardPreventCornerOverlap 属性在 v20 及之前的版本中为 CardView 添加填充,以防止卡片内容和圆角之间的交叉。

    cardUseCompatPadding 属性在 API v21+ 中添加了填充,以便与以前的版本具有相同的测量值。

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 2021-03-08
      • 2022-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多