cardview使用笔记

来源:互联网 发布:网络校时原理 编辑:程序博客网 时间:2024/05/12 15:46

demo图

首先导入
…\android-sdk\extras\android\support\v7\cardview
lib项目

xml

<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:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity"    >    <android.support.v7.widget.CardView        xmlns:cardview="http://schemas.android.com/apk/res-auto"          android:id="@+id/card_view"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="@dimen/activity_horizontal_margin"        android:layout_marginRight="@dimen/activity_horizontal_margin"        android:layout_marginTop="5dp"        android:layout_marginBottom="5dp"        cardview:cardCornerRadius="10dp"        cardview:cardBackgroundColor="@android:color/white"        cardview: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/ic_launcher"            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>

java

    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         cardView = (CardView) findViewById(R.id.card_view);              SeekBar seek1 = (SeekBar) findViewById(R.id.seek1);              SeekBar seek2 = (SeekBar) findViewById(R.id.seek2);              seek1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {                  @Override                  public void onProgressChanged(SeekBar seekBar, int i, boolean b) {                      if (b) {                         cardView.setCardElevation(i);//阴影shadow                    }                  }                  @Override                  public void onStartTrackingTouch(SeekBar seekBar) {                  }                  @Override                  public void onStopTrackingTouch(SeekBar seekBar) {                  }              });              seek2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {                  @Override                  public void onProgressChanged(SeekBar seekBar, int i, boolean b) {                      if (b) {                          cardView.setRadius(i);//圆角大小设置                      }                  }                  @Override                  public void onStartTrackingTouch(SeekBar seekBar) {                  }                  @Override                  public void onStopTrackingTouch(SeekBar seekBar) {                  }              });      }
0 0