A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

概述

    CardView继承自FrameLayout类,并且可以设置圆角和阴影,使得空间具有立体感,也可以包含其他的布局容器和控件。

基本使用

     使用之前必须先配置一下我们的build.gradle(Module.app),导入support-v7包:



  • dependencies {



  •     ...



  •     compile 'com.android.support:appcompat-v7:26.+'



  •     compile 'com.android.support:cardview-v7:26.+'



  • }


    看看简简单单的xml文件:



  • <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/image" />







  •             <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:textSize="14sp"



  •                     android:text="棒冰行动,公益传播设计夏令营" />



  •             </LinearLayout>



  •         </LinearLayout>



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


    效果图:


    没有在布局文件中定义圆角值跟阴影,是在MainActivity中配置的:



  •         mCardView = (CardView) findViewById(R.id.cardView);







  •         // 都可以在xml文件中设置



  •         mCardView.setRadius(20);//设置图片圆角的半径大小







  •         mCardView.setCardElevation(8);//设置阴影部分大小







  •         mCardView.setContentPadding(5,5,5,5);//设置图片距离阴影大小







  •         mCardView.setOnClickListener(new View.OnClickListener() {



  •             @Override



  •             public void onClick(View view) {



  •                 Toast.makeText(MainActivity.this, "按了", Toast.LENGTH_SHORT).show();



  •             }



  •         });


    到此,CardView的基本使用就完了,但是CardView还有更多属性



  •         cardBackgroundColor:设置背景色



  •         cardElevation:设置Z轴阴影(就是设置阴影)



  •         cardMaxElevation:设置Z轴最大高度值



  •         cardUseCompatPadding:是否使用CompatPadding



  •         cardPreventCornerOverlap:是否使用PreventCornerOverlap



  •         contentPadding:内容的padding



  •         contentPaddingLeft:内容的左padding



  •         右padding、上padding、底padding




0 个回复

您需要登录后才可以回帖 登录 | 加入黑马