随笔 - 245  文章 - 0  评论 - 11  阅读 - 50万

Android中的CardView使用

Android 5.0 版本中新增了CardView,CardView继承自FrameLayout类,并且可以设置圆角和阴影,使得控件具有立体性,也可以包含其他的布局容器和控件。

1.配置build.gradle

如果SDK低于5.0,我们仍旧要引入v7包。在build.gradle 中加入如下代码已自动导入 support-v7包。记得配置完再重新Build一下工程。

compile 'com.android.support:appcompat-v7:22.2.1‘
compile 'com.android.support:cardview-v7:22.1.0'

2.使用CardView实现如下效果:

 

布局如下:

复制代码
<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:id="@+id/cv_cardview"
        app:cardCornerRadius="20dp"
        app:cardElevation="20dp"
        android:layout_centerInParent="true">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/aa"
            android:scaleType="centerInside"/>
    </android.support.v7.widget.CardView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <SeekBar
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/sb_1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="控制圆角大小"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <SeekBar
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/sb_2"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="控制阴影大小"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <SeekBar
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/sb_3"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="控制图片间距大小"/>
    </LinearLayout>



</LinearLayout>
复制代码

重要属性:

app:cardCornerRadius 设置圆角的半径
app:cardElevation 设置阴影的半径
其它属性:
复制代码
        app:cardBackgroundColor=""设置背景色
        app:cardMaxElevation="" 设置Z轴最大高度值
        app:cardUseCompatPadding="" 是否使用CompatPadding
        app:cardPreventCornerOverlap="" 是否使用PreventCornerOverlap
        app:contentPadding="" 内容的Padding
        app:contentPaddingTop="" 内容的上Padding
        app:contentPaddingLeft="" 内容的左Padding
        app:contentPaddingRight="" 内容的右Padding
        app:contentPaddingBottom="" 内容的下Padding
复制代码

java代码:

复制代码
package com.example.cardviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

   /* app:cardBackgroundColor=""设置背景色
    app:cardMaxElevation="" 设置Z轴最大高度值
    app:cardUseCompatPadding="" 是否使用CompatPadding
    app:cardPreventCornerOverlap="" 是否使用PreventCornerOverlap
    app:contentPadding="" 内容的Padding
    app:contentPaddingTop="" 内容的上Padding
    app:contentPaddingLeft="" 内容的左Padding
    app:contentPaddingRight="" 内容的右Padding
    app:contentPaddingBottom="" 内容的下Padding*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final CardView mCardview = (CardView) findViewById(R.id.cv_cardview);
        SeekBar sb_1 = (SeekBar) findViewById(R.id.sb_1);
        SeekBar sb_2 = (SeekBar) findViewById(R.id.sb_2);
        SeekBar sb_3 = (SeekBar) findViewById(R.id.sb_3);

        sb_1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                mCardview.setRadius(i);//设置圆角半径
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        sb_2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                mCardview.setCardElevation(i);//设置阴影半径
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        sb_3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                mCardview.setContentPadding(i, i, i, i);//设置cardView中子控件和父控件的距离
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}
复制代码

完成

 
posted on   巫山老妖  阅读(29471)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示