Android: Installation and use of the latest version of CardView

A lot of software uses a card-like layout, which looks very beautiful. After checking, most of them are realized by the magical component of CardView.
Read most of the tutorials, most of them use this line of code to call the cardview package

import android.support.v7.widget.CardView

After spending a lot of energy, I still cannot successfully install the corresponding dependencies.
It was later discovered that the current method has changed due to the version update. The code should be changed to this line:

import androidx.cardview.widget.CardView;

After selecting, press Alt+Enter, it will automatically install the dependencies.
If it doesn’t work, install it manually
. Add a line of code under the build.gradle(app) file

implementation 'androidx.cardview:cardview:1.0.0'

After Sync Now, it was installed smoothly.

Try the effect:
activity_main.xml

<?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

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);//设置图片距离阴影大小
    }
}

The effect is as shown in the figure:
Insert picture description here
I will put more and more interesting content in my personal public account. Scan the picture above or search for "I have a trick" to find more and more interesting easter eggs.

Guess you like

Origin blog.csdn.net/qq1198768105/article/details/114609740