1. 程式人生 > >【Android studio】CardView 卡片佈局簡單學習 和 報錯的解決方法!

【Android studio】CardView 卡片佈局簡單學習 和 報錯的解決方法!

CardView 佈局 很常見,比如QQ 好友資訊(左滑動可見),貼吧的廣告推送等等

除了listview,今天我們來簡單學習cardview!

宣告:本文只說android studio的解決報錯方法,

Eclipse_Android 的看這裡:http://www.cnblogs.com/ChangFen/p/6268108.html

例子:

有道詞典:這種瀑布流 排序!


資訊面板的瀏覽:

1.什麼是CardView?

    卡片佈局

2.CardView常用屬性

    app:cardBackgroundColor這是設定背景顏色 
    app:cardCornerRadius這是設定圓角大小 
    app:cardElevation這是設定z軸的陰影 
    app:cardMaxElevation這是設定z軸的最大高度值 
    app:cardUseCompatPadding是否使用CompatPadding 
    app:cardPreventCornerOverlap是否使用PreventCornerOverlap 
    app:contentPadding 設定內容的padding 
    app:contentPaddingLeft 設定內容的左padding 
    app:contentPaddingTop 設定內容的上padding 
    app:contentPaddingRight 設定內容的右padding 
    app:contentPaddingBottom 設定內容的底padding


--------------------------------------------------------------------------------------------------------------------------------

一、程式碼組成:一個方法,一個xml,一張圖片sng.png 


效果如下,當前版本為:sdk 23


二、MainActivity.java

package com.open_open.android_cardview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.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);//設定圖片距離陰影大小
    }
}

三、activity_cardview.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="com.open_open.android_cardview.MainActivity">

    <android.support.v7.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="100dp">

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

            <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>
    </android.support.v7.widget.CardView>


</RelativeLayout>


四、報錯問題:MainActivity  無法識別:cardview ! 

右上角 - 一個放大鏡!


手動增加一行程式碼! (確定你有更新了對應 的 SDK 版本!)


回到ManiActivity.java 方法頁面,有一行提示:我們點選 Sync Now 進行同步!


等待一會,然後報錯資訊都會消失,

手動快捷鍵,打 cardview的V7包 ,即可!

以上感謝大家的幫助!謝謝!