CardView學(xué)習(xí)筆記

一. 什么是CardView?

1.1官方介紹

CardView擴(kuò)展 FrameLayout類并讓您能夠顯示卡片內(nèi)的信息,這些信息在整個平臺中擁有一致的呈現(xiàn)方式。CardView小部件可擁有陰影和圓角。

1.2效果圖

CardView里面嵌套TextView

二.用法

2.1使用步驟

  • 在build.gradle中加入
dependencies {
        compile 'com.android.support:cardview-v7:version'
}(version為版本號)
  • xml中使用布局
  • 注意:因為CardView繼承自FrameLayout,因此其子控件一般只有一個或者是<ViewGroup>
<android.support.v7.widget.CardView
        android:id="@+id/hello_world_card"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:cardCornerRadius="3dp"
        app:cardElevation="@dimen/cardview_default_elevation"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Hello World!"
            android:gravity="center"
            android:textSize="24sp"/>

    </android.support.v7.widget.CardView>
  • 添加點(diǎn)擊事件
mCardView = (CardView) findViewById(R.id.hello_world_card);
mCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"你點(diǎn)擊了CardView",Toast.LENGTH_SHORT).show();
        }
    });

三.xml屬性詳細(xì)介紹 官方鏈接

  • 注意:其中android.support.v7.cardview在xml中就是app(自定義布局的前綴)
android.support.v7.cardview:cardBackgroundColor
CardView的背景顏色.值為rgb或者argb顏色值(如#ffffff)
android.support.v7.cardview:cardCornerRadius
CardView的圓角弧度.值為dimension值(如4dp)
android.support.v7.cardview:cardElevation
CardView的陰影高度。值為dimension值(如4dp)
android.support.v7.cardview:cardMaxElevation
CardView的最大陰影高度。值為dimension值(如4dp),自己沒看出來有什么效果
android.support.v7.cardview:contentPadding
Cardview內(nèi)子控件與CardView邊緣的邊距。值為dimension值(如4dp)
android.support.v7.cardview:cardUseCompatPadding
在xml文件中設(shè)置內(nèi)邊距,V21+的版本和之前的版本仍舊具有一樣的計算方式
android.support.v7.cardview:cardPreventConrerOverlap
在xml文件中設(shè)置內(nèi)邊距,在V20和之前的版本中添加內(nèi)邊距,這個屬性為了防止內(nèi)容和邊角的重疊

四.源碼

  • MainActivity
public class MainActivity extends AppCompatActivity {

    private CardView mCardView;

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

        mCardView = (CardView) findViewById(R.id.hello_world_card);
        mCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"你點(diǎn)擊了CardView",Toast.LENGTH_SHORT).show();
            }
        });

    }
}
  • 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cn.foxnickel.recyclerview.MainActivity"
    android:padding="8dp">

    <android.support.v7.widget.CardView
        android:id="@+id/hello_world_card"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:cardCornerRadius="3dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardElevation="4dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Hello World!"
            android:gravity="center"
            android:textSize="24sp"/>

    </android.support.v7.widget.CardView>

</RelativeLayout>

五.參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容