【问题标题】:Are CardView and RecyclerView material design views?CardView 和 RecyclerView 是材料设计视图吗?
【发布时间】:2015-09-23 21:39:35
【问题描述】:

我只是想知道它们是否真的是 MD 视图,因为它们的名字是 喜欢 android.support.v7.widget.CardView.

5.0 及以上版本的 CardView 是否有不同的包名(类似于 android.MaterialDesign.CardView)?

【问题讨论】:

  • 只需使用 android.support.v7.widget.CardView...只有 20% 的 API 5.0+,所以... 80% 的其他人需要支持库。
  • 20% 的 android.support.v7.widget.CardView 有什么不同吗?
  • 不...甚至官方文档都告诉您使用支持:developer.android.com/training/material/lists-cards.html

标签: android material-design


【解决方案1】:
compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'com.android.support:support-v4:23.0.1'
    compile 'com.android.support:cardview-v7:23.0.1'

是的,它们是,要在您的应用中使用它们,您需要将这些依赖项添加到您的 gradle 文件中。

回收站视图

<android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

卡片视图

<android.support.v7.widget.CardView
        android:id="@+id/cv_custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:elevation="2dp"
        card_view:cardElevation="2dp"
        card_view:cardUseCompatPadding="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/person_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/custom_event_name"
                android:ellipsize="marquee"
                android:gravity="bottom|end"
                android:maxLines="3"
                android:padding="8dp"
                android:scrollHorizontally="true"
                android:textColor="@color/textColorSecondary"
                android:textSize="18sp" />

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

【讨论】:

    【解决方案2】:

    CardView 基本上是包含在支持库中的辅助小部件。即使在 Android 5.0 及更高版本中,您也必须使用 android.support.v7.widget.CardView。但是,在 5.0 及更高版本中,此小部件的绘制方式有所不同。

    来自官方文档:

    带有圆角背景和阴影的 FrameLayout。

    CardView 使用 L 上的海拔属性作为阴影并回退到 旧平台上的自定义影子实现。

    由于圆角剪裁成本高昂,因此在平台上 在 L 之前,CardView 不会剪辑与 圆角。相反,它添加了填充以避免这种交叉 (请参阅 setPreventCornerOverlap(boolean) 以更改此行为)。

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

    更多信息请参见Android Developers - CardView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      相关资源
      最近更新 更多