這篇文章將為大家詳細(xì)講解有關(guān)Android UI新組件怎么用,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括荔灣網(wǎng)站建設(shè)、荔灣網(wǎng)站制作、荔灣網(wǎng)頁制作以及荔灣網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,荔灣網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到荔灣省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
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ī)е?,你帶著錢,來一場(chǎng)說走就走的旅行") .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ì)操作下拉多少距離之后開始刷新數(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資源,否則將沒有效果。看下面app: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" android:layout_width="wrap_content" android:layout_height="wrap_content"></Spinner>
關(guān)于“Android UI新組件怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
網(wǎng)站名稱:AndroidUI新組件怎么用
當(dāng)前URL:http://chinadenli.net/article10/jggcgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、ChatGPT、企業(yè)建站、電子商務(wù)、定制開發(fā)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)