android用PopWindow做下拉框?qū)嵗a
最近在做下拉框,本來想用spinner,可是spinner達(dá)不到項(xiàng)目要求,跟同學(xué)同事問了一圈,都在用popwindow,網(wǎng)上看了一下,popwindow挺簡(jiǎn)單的,可定制性挺強(qiáng)的,符合我的要求,所以,借鑒網(wǎng)上看的代碼,自己擼了一遍。寫篇博客以防忘記。
首先,先寫個(gè)自定義布局,代碼如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="110dp" android:layout_height="wrap_content"> <LinearLayout android:layout_width="100dp" android:layout_height="wrap_content" android:background="@drawable/bg_circle_drop_down_qr_code" android:orientation="vertical" android:layout_marginRight="@dimen/padding_10" android:paddingBottom="0dp" android:paddingLeft="@dimen/padding_5" android:paddingRight="@dimen/padding_5" android:paddingTop="@dimen/padding_5"> <LinearLayout android:id="@+id/lin_scan_qr_code" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="horizontal" android:paddingBottom="@dimen/padding_5" android:paddingTop="@dimen/padding_5"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_circle_scan_qr_code" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/padding_10" android:gravity="center" android:text="掃一掃" android:textColor="@color/color_white" android:textSize="@dimen/text_16" /> </LinearLayout> <View android:layout_width="wrap_content" android:layout_height="1px" android:layout_marginLeft="@dimen/padding_3" android:layout_marginRight="@dimen/padding_3" android:background="@color/color_white" /> <LinearLayout android:id="@+id/lin_my_qr_code" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="horizontal" android:paddingBottom="@dimen/padding_5" android:paddingTop="@dimen/padding_5"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_circle_my_qr_code" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/padding_10" android:gravity="center" android:text="二維碼" android:textColor="@color/color_white" android:textSize="@dimen/text_16" /> </LinearLayout> </LinearLayout> </LinearLayout>
第二步,在代碼中定義popwindow樣式,綁定點(diǎn)擊事件,代碼如下:
// // 獲取自定義布局文件pop.xml的視圖 View customView = getActivity().getLayoutInflater().inflate(R.layout.lay_circle_pop_drop_down_qr_code, null, false); // 創(chuàng)建PopupWindow實(shí)例,200,150分別是寬度和高度 mQrCodePopWindow = new PopupWindow(customView, CommonUtil.dipToPx(getContext(),110), ViewGroup.LayoutParams.WRAP_CONTENT,true); // 設(shè)置動(dòng)畫效果 [R.style.AnimationFade 是自己事先定義好的] // popupwindow.setAnimationStyle(R.style.AnimationFade); // popupwindow.setTouchable(true); // popupwindow.setOutsideTouchable(true); mQrCodePopWindow.setBackgroundDrawable(new BitmapDrawable()); customView.findViewById(R.id.lin_scan_qr_code).setOnClickListener(v -> { ToastUtil.show(getContext(),"掃一掃"); dismissQrCodePopWindow(); }); customView.findViewById(R.id.lin_my_qr_code).setOnClickListener(v -> ToastUtil.show(getContext(),"二維碼"));
注意,代碼中的true為setFoucusable,如要點(diǎn)擊空白處隱藏popwindow的話,setFocusable(true)和setBackground()兩者必不可少(親測(cè))。
最后,為空間添加點(diǎn)擊事件,控制下拉框的顯示隱藏,代碼如下:
@OnClick(R.id.lin_top_right) public void onClick(View v) { if (mQrCodePopWindow != null&& mQrCodePopWindow.isShowing()) { mQrCodePopWindow.dismiss(); } else { initQrCodePopWindow(); mQrCodePopWindow.showAsDropDown(v); } }
(由于暫時(shí)沒有發(fā)現(xiàn)好的動(dòng)畫效果,所以沒有添加動(dòng)畫,如果大家有發(fā)現(xiàn)好的動(dòng)畫,還請(qǐng)告知一二,在此謝過)
效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用百度地圖Android sdk高仿微信發(fā)送位置功能及遇到的問題
這篇文章給大家介紹了利用百度地圖Android sdk高仿微信發(fā)送位置功能,在實(shí)現(xiàn)此功能的時(shí)候遇到點(diǎn)小問題,下面小編給大家列出來,需要的朋友參考下吧2017-12-12Android ListView中動(dòng)態(tài)顯示和隱藏Header&Footer的方法
這篇文章主要介紹了Android ListView中動(dòng)態(tài)顯示和隱藏Header&Footer的方法及footer的兩種正確使用方法,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,對(duì)listview header footer相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-08-08Flutter banner_view 輪播圖的使用及實(shí)現(xiàn)代碼
這篇文章主要介紹了Flutter banner_view 輪播圖的使用及實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析
StrictMode意思為嚴(yán)格模式,是用來檢測(cè)程序中違例情況的開發(fā)者工具。最常用的場(chǎng)景就是檢測(cè)主線程中本地磁盤和網(wǎng)絡(luò)讀寫等耗時(shí)的操作。這篇文章給大家介紹Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析,感興趣的朋友一起看看吧2018-01-01Android?studio實(shí)現(xiàn)動(dòng)態(tài)背景頁面
這篇文章主要為大家詳細(xì)介紹了Android?studio實(shí)現(xiàn)動(dòng)態(tài)背景頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android使用SmsManager實(shí)現(xiàn)短信發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了Android使用SmsManager實(shí)現(xiàn)短信發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android 獲取服務(wù)器與客戶端時(shí)差的實(shí)例代碼
下面小編就為大家分享一篇Android 獲取服務(wù)器與客戶端時(shí)差的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android滾動(dòng)菜單ListView實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了Android滾動(dòng)菜單ListView實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Android系統(tǒng)檢測(cè)程序內(nèi)存占用各種方法
這篇文章主要介紹了Android系統(tǒng)檢測(cè)程序內(nèi)存占用各種方法,本文講解了檢查系統(tǒng)總內(nèi)存、檢查某個(gè)程序的各類型內(nèi)存占用、檢查程序狀態(tài)、檢查程序各部分的內(nèi)存占用等內(nèi)容,需要的朋友可以參考下2015-03-03