Popupwindow 的簡單實(shí)用案例(顯示在控件下方)
第一步:
private PopupWindow mPopupWindow;
第二步:寫一個(gè)popupwindow的布局文件XML
<?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:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#669E9E9E"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#E4E4E4" > <TextView android:id="@+id/popupwindow_Jan" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="一月份" android:gravity="center" /> <TextView android:id="@+id/popupwindow_Feb" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="二月份" android:gravity="center" /> <TextView android:id="@+id/popupwindow_Mar" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="三月份" android:gravity="center" /> </LinearLayout> </RelativeLayout> </LinearLayout>
第三步:在Activity寫代碼
public void onClick(View v) { switch (v.getId()) { case R.id.home_travel_modes_yuefen_textview: showPopupWindow(v); break; case R.id.popupwindow_Jan: showToastMsg("一月份"); break; case R.id.popupwindow_Feb: showToastMsg("二月份"); break; default: break; } public void showPopupWindow(View v){ View contentView = LayoutInflater.from(HomeTravelModesActivity.this).inflate(R.layout.home_popuplayout, null); TextView JanText = (TextView)contentView.findViewById(R.id.popupwindow_Jan); TextView FebText = (TextView)contentView.findViewById(R.id.popupwindow_Feb); TextView MarText = (TextView)contentView.findViewById(R.id.popupwindow_Mar); JanText.setOnClickListener(this); FebText.setOnClickListener(this); MarText.setOnClickListener(this); final PopupWindow popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, 300, true); popupWindow.setTouchable(true); // popupWindow.setTouchInterceptor(new View.OnTouchListener() { // // @Override // public boolean onTouch(View v, MotionEvent event) { // // Log.i("mengdd", "onTouch : "); // // return false; // // 這里如果返回true的話,touch事件將被攔截 // // 攔截后 PopupWindow的onTouchEvent不被調(diào)用,這樣點(diǎn)擊外部區(qū)域無法dismiss // } // }); // 如果不設(shè)置PopupWindow的背景,無論是點(diǎn)擊外部區(qū)域還是Back鍵都無法dismiss彈框 // 我覺得這里是API的一個(gè)bug popupWindow.setBackgroundDrawable(getResources().getDrawable( R.mipmap.ic_launcher)); // 設(shè)置好參數(shù)之后再show popupWindow.showAsDropDown(v); }
注:
若在Activity的onCreate()方法中直接寫彈出PopupWindow()方法報(bào)錯(cuò),因?yàn)锳ctivity沒有完全啟動(dòng)是不能彈出PopupWindow的,那我們只需要在Activity完全啟動(dòng)后在彈出PopupWindow就行了。
重寫一下onWindowFocusChanged()方法:
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); //彈出PopupWindow的具體代碼 }
以上這篇Popupwindow 的簡單實(shí)用案例(顯示在控件下方)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程簡單獲取網(wǎng)絡(luò)上的圖片
這篇文章主要介紹了Android編程簡單獲取網(wǎng)絡(luò)上的圖片,結(jié)合實(shí)例形式分析了Android獲取網(wǎng)絡(luò)圖片及加載顯示的相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下2016-10-10android-獲取網(wǎng)絡(luò)時(shí)間、獲取特定時(shí)區(qū)時(shí)間、時(shí)間同步的方法
本篇文章主要介紹了android-獲取網(wǎng)絡(luò)時(shí)間、獲取特定時(shí)區(qū)時(shí)間、時(shí)間同步,小編覺得不錯(cuò),現(xiàn)在就分享給大家,有興趣的可以了解一下。2016-12-12Android調(diào)用系統(tǒng)默認(rèn)瀏覽器訪問的方法
這篇文章主要介紹了Android調(diào)用系統(tǒng)默認(rèn)瀏覽器訪問的方法的相關(guān)資料,需要的朋友可以參考下2016-03-03Android Studio 通過登錄功能介紹SQLite數(shù)據(jù)庫的使用流程
SQLite是一款輕型的數(shù)據(jù)庫,是遵守ACID的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它包含在一個(gè)相對(duì)小的C庫中。這篇文章主要介紹了Android Studio 通過登錄功能介紹SQLite數(shù)據(jù)庫的使用流程,需要的朋友可以參考下2018-09-09Android使用動(dòng)畫設(shè)置ProgressBar進(jìn)度的方法
這篇文章主要為大家詳細(xì)介紹了Android使用動(dòng)畫設(shè)置ProgressBar進(jìn)度的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Android實(shí)現(xiàn)退出界面彈出提示對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)退出界面彈出提示對(duì)話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-1220.5 語音合成(百度2016年2月29日發(fā)布的tts引擎)
編寫手機(jī)App時(shí),有時(shí)需要使用文字轉(zhuǎn)語音(Text to Speech)的功能,比如開車時(shí)閱讀收到的短信、導(dǎo)航語音提示、界面中比較重要的信息通過語音強(qiáng)調(diào)2016-03-03Android實(shí)現(xiàn)的數(shù)字格式化用法示例
這篇文章主要介紹了Android實(shí)現(xiàn)的數(shù)字格式化用法,結(jié)合實(shí)例形式分析了Android數(shù)學(xué)運(yùn)算中數(shù)字格式化輸出的相關(guān)技巧,需要的朋友可以參考下2016-08-08