前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Android:最新版CardView的安装和使用

Android:最新版CardView的安装和使用

作者头像
zstar
发布2022-06-14 10:28:57
发布2022-06-14 10:28:57
90400
代码可运行
举报
文章被收录于专栏:往期博文往期博文
运行总次数:0
代码可运行

很多软件都使用了卡片式的布局,看上去非常美观。查了一下,大多是采用CardView这个神奇的组件实现。 翻阅大部分教程,大多数都是采用这行代码来调用cardview包

代码语言:javascript
代码运行次数:0
运行
复制
import android.support.v7.widget.CardView

耗费了很多精力,我还是无法成功安装相应的依赖。 后来发现,由于版本更新,现在的方式已经改变。代码应该改为这行:

代码语言:javascript
代码运行次数:0
运行
复制
import androidx.cardview.widget.CardView;

选中之后按Alt+回车,它会自动安装依赖 如果不行就手动安装 在build.gradle(app)文件下加一行代码

代码语言:javascript
代码运行次数:0
运行
复制
implementation 'androidx.cardview:cardview:1.0.0'

Sync Now 之后就顺利安装好了

试试效果: activity_main.xml

代码语言:javascript
代码运行次数:0
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="10dp"
    tools:context=".MainActivity">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp">

            <ImageView
                android:layout_width="150dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:scaleType="centerCrop"
                android:src="@drawable/wdd"></ImageView>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:text="欢迎关注 我有一计"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:text="更多更好玩的奇技淫巧" />
            </LinearLayout>
        </LinearLayout>
    </androidx.cardview.widget.CardView>


</RelativeLayout>

MainActivity.java

代码语言:javascript
代码运行次数:0
运行
复制
package cn.edu.cdut.cardview;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

public class MainActivity extends AppCompatActivity {

    private CardView cardView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cardView = (CardView) findViewById(R.id.cardView);

        cardView.setRadius(8);//设置图片圆角的半径大小

        cardView.setCardElevation(8);//设置阴影部分大小

        cardView.setContentPadding(5, 5, 5, 5);//设置图片距离阴影大小
    }
}

效果如图所示:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-03-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档