Android UI新組件學(xué)習(xí)和使用
今天來(lái)學(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ī)е?,你帶著錢(qián),來(lái)一場(chǎng)說(shuō)走就走的旅行")
.setNegativeButton("取消", null)
.setPositiveButton("確定", null)
.setTitle("Material Design Dialog")
.show();
}
是不是很贊,和之前的Dialog使用無(wú)任何差別,媽媽再也不用擔(dān)心我使用Material Dialog對(duì)話框了。
SwipeRefreshLayout

原來(lái)谷歌已經(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)聽(tīng)事件
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è)置下拉多少距離之后開(kāi)始刷新數(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ì)操作下拉多少距離之后開(kāi)始刷新數(shù)據(jù)
總結(jié):當(dāng)然 SwipeRefreshLayout 組件有很多不足之處,比如沒(méi)有上拉刷新這個(gè)功能,不過(guò)網(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)成的組件,你只需簡(jiǎ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 廢墟的樹(shù)"
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 廢墟的樹(shù)"
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 廢墟的樹(shù)"
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 廢墟的樹(shù)"
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 廢墟的樹(shù)"
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 廢墟的樹(shù)"
android:textSize="20sp"
android:textStyle="bold" />
</android.support.v7.widget.LinearLayoutCompat>
LinearLayoutCompat其實(shí)就是LinerLayout組件,只是為了兼容低版本,所以你必須的引用 V7包下面的LinearLayoutCompat。 LinearLayoutCompat除了擁有LinerLayout原本的屬性之外,主要有如下幾種屬性來(lái)實(shí)現(xiàn) 間隔線效果。
app:divider=”@drawable/line” 給分隔線設(shè)置顏色,這里你需要在drawable在定義shape資源,否則將沒(méi)有效果??聪旅鎍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é):以后你還需要自己畫(huà)分割線么?看完LinearLayoutCompat組件是不是很高興?。。」?/p>
ListPopupWindow
PopupWindow的簡(jiǎn)單實(shí)用,無(wú)需更多的去自定義,獲取去確定PopupWindow的位置等,你只需要使用ListPopupWindow就能滿足你簡(jiǎn)單的 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)聽(tīng)事件
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的位置來(lái)顯示,
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è)置彈出框的大小
不用解釋了吧!代碼都有注釋。望君自己研究然后去手寫(xiě)代碼,這樣學(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)用程序組件基本編寫(xiě)方法
- Android開(kāi)發(fā)中Button組件的使用
- Android Jetpack架構(gòu)組件 ViewModel詳解
- Android ListView UI組件使用說(shuō)明
- android自定義組件實(shí)現(xiàn)儀表計(jì)數(shù)盤(pán)
- Android中butterknife的使用與自動(dòng)化查找組件插件詳解
- Android開(kāi)發(fā)之組件GridView簡(jiǎn)單使用方法示例
- Android列表組件ListView使用詳解之動(dòng)態(tài)加載或修改列表數(shù)據(jù)
- Android四大組件之Service詳解
- Android框架組件Lifecycle的使用詳解
- 詳解Android的四大應(yīng)用程序組件
相關(guān)文章
Android Studio 引用外部依賴時(shí)報(bào)錯(cuò)的解決方法
這篇文章主要介紹了Android Studio報(bào)錯(cuò)Unable to resolve dependency for':app@release/compileClasspath':無(wú)法引用任何外部依賴的解決辦法,需要的朋友可以參考下2018-01-01
android高仿小米時(shí)鐘(使用Camera和Matrix實(shí)現(xiàn)3D效果)
這篇文章主要介紹了android高仿小米時(shí)鐘(使用Camera和Matrix實(shí)現(xiàn)3D效果),非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01
簡(jiǎn)單實(shí)用的Android studio 調(diào)試技巧
這篇文章主要介紹了簡(jiǎn)單實(shí)用的Android studio 調(diào)試技巧的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android編程之?dāng)?shù)據(jù)庫(kù)的創(chuàng)建方法詳解
這篇文章主要介紹了Android編程之?dāng)?shù)據(jù)庫(kù)的創(chuàng)建方法,結(jié)合實(shí)例形式分析了Android數(shù)據(jù)庫(kù)創(chuàng)建的步驟、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
Android中使用am命令實(shí)現(xiàn)在命令行啟動(dòng)程序詳解
這篇文章主要介紹了Android中使用am命令實(shí)現(xiàn)在命令行啟動(dòng)程序詳解,本文詳細(xì)講解了am命令的語(yǔ)法,然后給出了啟動(dòng)內(nèi)置程序的操作實(shí)例,需要的朋友可以參考下2015-04-04
Android開(kāi)發(fā)使用Databinding實(shí)現(xiàn)關(guān)注功能mvvp
這篇文章主要為大家介紹了Android開(kāi)發(fā)使用Databinding實(shí)現(xiàn)關(guān)注功能mvvp示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android Studio Kotlin代碼和java代碼相互轉(zhuǎn)化實(shí)例
這篇文章主要介紹了Android Studio Kotlin代碼和java代碼相互轉(zhuǎn)化實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
android效果TapBarMenu繪制底部導(dǎo)航欄的使用方式示例
本篇文章主要介紹了android效果TapBarMenu繪制底部導(dǎo)航欄的使用方式,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Android 狀態(tài)欄虛擬導(dǎo)航鍵透明效果的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 狀態(tài)欄虛擬導(dǎo)航鍵透明效果的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03

