Android UI新組件學(xué)習(xí)和使用
今天來學(xué)習(xí)總結(jié)一下,Android 后添加的一些新的組件和UI效果,Material Dialog,SwipeRefreshLayout,ListPopupWindow,PopupMenu等。
Material Dialog
你還在為使用 Material Dialog 去引用第三方的library包么?現(xiàn)在告訴你一個(gè)好消息,其實(shí)Android 在V7包里面已經(jīng)實(shí)現(xiàn)了 Material 風(fēng)格的對(duì)話框,并且兼容到底版本了。你只需要在你的代碼中使用V7中的Dialog即可實(shí)現(xiàn)以上圖片效果了。代碼如下
private void showDialog1() { android.support.v7.app.AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("讓我們一起飛,我?guī)е悖銕еX,來一場說走就走的旅行") .setNegativeButton("取消", null) .setPositiveButton("確定", null) .setTitle("Material Design Dialog") .show(); }
是不是很贊,和之前的Dialog使用無任何差別,媽媽再也不用擔(dān)心我使用Material Dialog對(duì)話框了。
SwipeRefreshLayout
原來谷歌已經(jīng)實(shí)現(xiàn)了 Material Design 風(fēng)格的下拉刷新組件,這個(gè)新的組件SwipeRefreshLayout是ViewGroup在V4包下面,你只需按照如下使用:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/swipe_container" android:layout_width="match_parent" android:layout_height="match_parent"> <!--可滑動(dòng)的組件,比如ScrollView,ListView,GridView,等--> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <!--添加自己的內(nèi)容--> </ScrollView> </android.support.v4.widget.SwipeRefreshLayout>
SwipeRefreshLayout組件下包裹一個(gè)可滑動(dòng)的組件即可實(shí)現(xiàn)下拉刷新效果。然后在Java代碼中使用如下:
swipeRefreshLayout = findView(R.id.swipe_container); //設(shè)置下拉刷新監(jiān)聽事件 swipeRefreshLayout.setOnRefreshListener(this); //設(shè)置進(jìn)度條的顏色 swipeRefreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN); //設(shè)置圓形進(jìn)度條大小 swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE); //設(shè)置進(jìn)度條背景顏色 swipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.DKGRAY); //設(shè)置下拉多少距離之后開始刷新數(shù)據(jù) swipeRefreshLayout.setDistanceToTriggerSync(50);
其中包括以下常用方法:
setColorSchemeColors() 設(shè)置進(jìn)度條顏色,可設(shè)置多個(gè)值,進(jìn)度條顏色在這多個(gè)顏色值之間變化setSize() 設(shè)置下拉出現(xiàn)的圓形進(jìn)度條的大小,有兩個(gè)值:SwipeRefreshLayout.DEFAULT 和 SwipeRefreshLayout.LARGEsetProgressBackgroundColorSchemeColor()設(shè)置圓形進(jìn)度條背景顏色。setDistanceToTriggerSync() 設(shè)置手勢操作下拉多少距離之后開始刷新數(shù)據(jù)
總結(jié):當(dāng)然 SwipeRefreshLayout 組件有很多不足之處,比如沒有上拉刷新這個(gè)功能,不過網(wǎng)上已經(jīng)有人實(shí)現(xiàn)了這一效果,想要的可以自己網(wǎng)上搜一把吧。
LinearLayoutCompat
最近在V7包中突然發(fā)現(xiàn) LinearLayoutCompat 組件,處于好奇,百度了一把這個(gè)組件的作用:用于給LinerLayout 中的子元素item之間設(shè)置間隔線的,效果圖如下:
你還在為給每個(gè)LinerLayout 的item元素添加分割線煩惱么?告訴你,不用煩惱啦!android 給你現(xiàn)成的組件,你只需簡單配置即可。代碼參考如下:
<android.support.v7.widget.LinearLayoutCompat android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center|center_horizontal" android:orientation="vertical" app:divider="@drawable/line" app:dividerPadding="25dp" app:showDividers="middle|beginning|end"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="10dp" android:text="CSDN 廢墟的樹" android:textSize="20sp" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="10dp" android:text="CSDN 廢墟的樹" android:textSize="20sp" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="10dp" android:text="CSDN 廢墟的樹" android:textSize="20sp" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="10dp" android:text="CSDN 廢墟的樹" android:textSize="20sp" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="10dp" android:text="CSDN 廢墟的樹" android:textSize="20sp" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="10dp" android:text="CSDN 廢墟的樹" android:textSize="20sp" android:textStyle="bold" /> </android.support.v7.widget.LinearLayoutCompat>
LinearLayoutCompat其實(shí)就是LinerLayout組件,只是為了兼容低版本,所以你必須的引用 V7包下面的LinearLayoutCompat。 LinearLayoutCompat除了擁有LinerLayout原本的屬性之外,主要有如下幾種屬性來實(shí)現(xiàn) 間隔線效果。
app:divider=”@drawable/line” 給分隔線設(shè)置顏色,這里你需要在drawable在定義shape資源,否則將沒有效果??聪旅鎍pp:dividerPadding=”25dp” 給分隔線設(shè)置距離左右邊距的距離。app:showDividers=”middle|beginning|end” 分隔線顯示的位置,有四種參數(shù)值:middle 每個(gè)item之間,beginning最頂端顯示分隔線,end 最底端顯示分隔線,none不顯示間隔線。
注意 這三個(gè)屬性需要使用 xmlns:app=”http://schemas.android.com/apk/res-auto” 命名空間
app:divider=”@drawable/line” 的資源代碼如下:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/material_blue_grey_800" /> <!--需要設(shè)置高度,否則不顯示--> <size android:height="1px" /> </shape>
總結(jié):以后你還需要自己畫分割線么?看完LinearLayoutCompat組件是不是很高興?。?!哈哈哈
ListPopupWindow
PopupWindow的簡單實(shí)用,無需更多的去自定義,獲取去確定PopupWindow的位置等,你只需要使用ListPopupWindow就能滿足你簡單的 PopupWindow 彈出框的使用了。直接上代碼:
public void showListPopup(View view) { String items[] = {"item1", "item2", "item3", "item4", "item5"}; final ListPopupWindow listPopupWindow = new ListPopupWindow(this); //設(shè)置ListView類型的適配器 listPopupWindow.setAdapter(new ArrayAdapter<String>(SwipeRefreshActivity.this, android.R.layout.simple_list_item_1, items)); //給每個(gè)item設(shè)置監(jiān)聽事件 listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(SwipeRefreshActivity.this, "the position is" + position, Toast.LENGTH_SHORT).show(); // listPopupWindow.dismiss(); } }); //設(shè)置ListPopupWindow的錨點(diǎn),也就是彈出框的位置是相對(duì)當(dāng)前參數(shù)View的位置來顯示, listPopupWindow.setAnchorView(view); //ListPopupWindow 距錨點(diǎn)的距離,也就是相對(duì)錨點(diǎn)View的位置 listPopupWindow.setHorizontalOffset(100); listPopupWindow.setVerticalOffset(100); //設(shè)置對(duì)話框的寬高 listPopupWindow.setWidth(300); listPopupWindow.setHeight(600); listPopupWindow.setModal(false); listPopupWindow.show(); }
根據(jù)以上代碼,你可以做如下事情:
listPopupWindow.setAnchorView(view); 設(shè)置彈出框顯示的位置listPopupWindow.setHorizontalOffset(100);距離錨點(diǎn)View水平距離listPopupWindow.setVerticalOffset(100); 距離錨點(diǎn)View的垂直距離listPopupWindow.setWidth(300);設(shè)置彈出框的大小
不用解釋了吧!代碼都有注釋。望君自己研究然后去手寫代碼,這樣學(xué)習(xí)更快。
PopupMenu
菜單彈出框,效果如下:
代碼如下:
public void showPopupMenu(View view) { //參數(shù)View 是設(shè)置當(dāng)前菜單顯示的相對(duì)于View組件位置,具體位置系統(tǒng)會(huì)處理 PopupMenu popupMenu = new PopupMenu(this, view); //加載menu布局 popupMenu.getMenuInflater().inflate(R.menu.menu_main, popupMenu.getMenu()); //設(shè)置menu中的item點(diǎn)擊事件 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { return false; } }); //設(shè)置popupWindow消失的點(diǎn)擊事件 popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() { @Override public void onDismiss(PopupMenu menu) { } }); popupMenu.show(); }
總結(jié):PopupMenu 相對(duì)ListPopupWindow可定制化比較少。
Spinner
流行風(fēng)格的下拉類別組件。你只需要在XML布局中使用 新的style主題即可實(shí)現(xiàn)如上效果
<Spinner android:id="@+id/spinner" style="@android:style/Widget.Holo.Light.Spinner" android:layout_width="wrap_content" android:layout_height="wrap_content"></Spinner>
后續(xù)如有發(fā)現(xiàn)好用的,美觀的UI組件會(huì)繼續(xù)添加。
- Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
- Android的Service應(yīng)用程序組件基本編寫方法
- Android開發(fā)中Button組件的使用
- Android Jetpack架構(gòu)組件 ViewModel詳解
- Android ListView UI組件使用說明
- android自定義組件實(shí)現(xiàn)儀表計(jì)數(shù)盤
- Android中butterknife的使用與自動(dòng)化查找組件插件詳解
- Android開發(fā)之組件GridView簡單使用方法示例
- Android列表組件ListView使用詳解之動(dòng)態(tài)加載或修改列表數(shù)據(jù)
- Android四大組件之Service詳解
- Android框架組件Lifecycle的使用詳解
- 詳解Android的四大應(yīng)用程序組件
相關(guān)文章
Android HorizontalScrollView左右滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android HorizontalScrollView左右滑動(dòng)效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android獲得內(nèi)/外置存儲(chǔ)卡路徑的方法
我們知道Android上一般都有外置的存儲(chǔ)卡,內(nèi)置存儲(chǔ)卡路徑大家都知道怎么獲得的。那么如何獲取外置存儲(chǔ)卡的位置呢?下面小編通過本文給大家分享下2017-01-01Android Studio 3.0 新功能全面解析和舊項(xiàng)目適配問題
Android Studio是Android的官方IDE。接下來通過本文給大家分享Android Studio 3.0 新功能全面解析和舊項(xiàng)目適配問題,需要的朋友可以參考下2017-11-11Android網(wǎng)絡(luò)請(qǐng)求庫android-async-http介紹
這篇文章主要介紹了Android網(wǎng)絡(luò)請(qǐng)求庫android-async-http介紹,本文講解了android-async-http的概念、特征以及使用實(shí)例,需要的朋友可以參考下2015-06-06Android自定義gridView仿頭條頻道拖動(dòng)管理功能
這篇文章主要介紹了Android自定義gridView仿頭條頻道拖動(dòng)管理功能,本文通過實(shí)例代碼效果圖展示給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12Android Studio 新手入門教程(一)基本設(shè)置圖解
這篇文章主要介紹了Android Studio 新手入門教程(一)基本設(shè)置圖解,需要的朋友可以參考下2017-12-12ListView-添加item的事件監(jiān)聽實(shí)例
下面小編就為大家?guī)硪黄狶istView-添加item的事件監(jiān)聽實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07Android使用Retrofit2.0技術(shù)仿微信發(fā)說說
這篇文章主要為大家詳細(xì)介紹了Android使用Retrofit2.0技術(shù)仿微信發(fā)說說,實(shí)現(xiàn)拍照,選圖庫,多圖案上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android編程中常用適配器及自定義適配器用法實(shí)例分析
這篇文章主要介紹了Android編程中常用適配器及自定義適配器用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android中適配器的概念、功能及自定義適配器的相關(guān)使用技巧,需要的朋友可以參考下2015-11-11