Android CardView的使用

一.概述

CardView是support.v7包中的一个类,继承自FrameLayout,今天来看看如何使用。首先我们需要导入相关的库
Android CardView的使用_第1张图片

二.代码

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_green_dark" xmlns:card_view="http://schemas.android.com/apk/res-auto" >
    <android.support.v7.widget.CardView  android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="wrap_content" card_view:cardBackgroundColor="@android:color/white" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" card_view:cardCornerRadius="10dp" card_view:cardElevation="24dp">
        <RelativeLayout  android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin" >
            <ImageView  android:id="@+id/img" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/cat" android:layout_centerHorizontal="true" android:scaleType="fitCenter" />
            <TextView  android:id="@+id/text_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/img" android:layout_centerHorizontal="true" android:text="This is a card view" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
    <SeekBar  android:layout_below="@+id/card_view" android:id="@+id/seek1" android:layout_marginTop="10dp" android:layout_width="150dp" android:layout_height="30dp" />
    <SeekBar  android:layout_below="@+id/seek1" android:id="@+id/seek2" android:layout_marginTop="10dp" android:layout_width="150dp" android:layout_height="30dp" />
</RelativeLayout>

首先我们引入了命名空间:

xmlns:card_view="http://schemas.android.com/apk/res-auto"

然后设置背景颜色为白色

 card_view:cardBackgroundColor="@android:color/white"

设备边角的弧度以及阴影宽度

 card_view:cardCornerRadius="10dp"
        card_view:cardElevation="24dp"
public class CardViewActivity extends AppCompatActivity {
    private CardView cardView;
    private SeekBar seekBar1;
    private SeekBar seekBar2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cardView = (CardView) findViewById(R.id.card_view);
        seekBar1 = (SeekBar) findViewById(R.id.seek1);
        seekBar2 = (SeekBar) findViewById(R.id.seek2);
        seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    cardView.setCardElevation(progress);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    cardView.setRadius(progress);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }
}

效果图

Android CardView的使用_第2张图片

你可能感兴趣的:(Android CardView的使用)