CardView也是一个容器类布局,开发者可以定义卡片的大小与视图的高度
,并设置圆角的角度。首先你需要在项目中引用com.android.support:
cardview-v7:21.+的依赖。然后在布局文件中引入一个新的名字空间--
xmlns:card_view=http://schemas.android.com/apk/res-auto,这样才可以通过
自定义的名字来引用它的两个属性。card_view:cardBackgroundColor="颜色"和
card_view:cardCornerRadius="8dp"。第一个设置背景颜色,第二个设置圆角的角度。
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
tools:context="com.shuaijie.jiang.recycleviewdemo.MainActivity">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="100dp"
card_view:cardBackgroundColor="#1f38f7"
card_view:cardCornerRadius="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="CardView"
android:textColor="#ffffff" />
</android.support.v7.widget.CardView>
,并设置圆角的角度。首先你需要在项目中引用com.android.support:
cardview-v7:21.+的依赖。然后在布局文件中引入一个新的名字空间--
xmlns:card_view=http://schemas.android.com/apk/res-auto,这样才可以通过
自定义的名字来引用它的两个属性。card_view:cardBackgroundColor="颜色"和
card_view:cardCornerRadius="8dp"。第一个设置背景颜色,第二个设置圆角的角度。
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
tools:context="com.shuaijie.jiang.recycleviewdemo.MainActivity">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="100dp"
card_view:cardBackgroundColor="#1f38f7"
card_view:cardCornerRadius="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="CardView"
android:textColor="#ffffff" />
</android.support.v7.widget.CardView>
</LinearLayout>
在使用CardView时,如果给CardView添加点击效果,要在里面的布局中加,不要在CardView上设置属性。
例如
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" card_view:cardBackgroundColor="@color/colorAccent" card_view:cardCornerRadius="10dp" card_view:cardElevation="10dp"> <TextView android:id="@+id/rv_item_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" android:gravity="center" android:textSize="20sp" /> </android.support.v7.widget.CardView>在CardView中TextView加上背景,就会出现水波纹的效果。
226

被折叠的 条评论
为什么被折叠?



