溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android控件CardView實現卡片布局

發布時間:2020-10-07 21:27:22 來源:腳本之家 閱讀:298 作者:簡單不一定不好 欄目:移動開發

CardView介紹

CardView是Android 5.0系統引入的控件,相當于FragmentLayout布局控件然后添加圓角及陰影的效果;CardView被包裝為一種布局,并且經常在ListView和RecyclerView的Item布局中,作為一種容器使用。CardView應該被使用在顯示層次性的內容時;在顯示列表或網格時更應該被選擇,因為這些邊緣可以使得用戶更容易去區分這些內容。

使用

先看效果

Android控件CardView實現卡片布局

首先在build.gradle文件添加依賴庫

dependencies {
 compile fileTree(include: ['*.jar'], dir: 'libs')
 testCompile 'junit:junit:4.12'
 compile 'com.android.support:appcompat-v7:24.2.0'
 compile 'com.android.support:cardview-v7:24.2.0'
}

布局文件main.html文件下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <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>

</LinearLayout>

在MainActivity.java下文件

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);//設置圖片距離陰影大小
 }
}

好,已結束CardView難度不大,當是實用性及及效果是非常棒的,值得你擁有!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女