Android開(kāi)發(fā)之PopupWindow創(chuàng)建彈窗、對(duì)話框的方法詳解
本文實(shí)例講述了Android開(kāi)發(fā)之PopupWindow創(chuàng)建彈窗、對(duì)話框的方法。分享給大家供大家參考,具體如下:
簡(jiǎn)介:
PopupWindow 可創(chuàng)建類(lèi)似對(duì)話框風(fēng)格的窗口
效果:
使用方法:
使用PopupWindow 創(chuàng)建對(duì)話框風(fēng)格的串口秩序如下兩步即可:
1. PopupWindow 的構(gòu)造器創(chuàng)建PopupWindow對(duì)象
2. PopupWindow 的showAsDropDown()
將其顯示效果設(shè)置為下拉顯示
3. PopupWindow 的showAtLoacation()
方法將PopupWindow()
在指定位置顯示出來(lái)
下拉顯示效果:
具體實(shí)現(xiàn)方法:
public class MainActivity extends Activity { private PopupWindow popupWindow; private View root; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); root = this.getLayoutInflater().inflate(R.layout.cell,null);//add cell.xml above you mainActivity window popupWindow = new PopupWindow(root,560,700);//create a popupWindow object root.findViewById(R.id.button01).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //close the popupWindow popupWindow.dismiss(); } }); } public void send(View source){ //set the location of PopupWindow popupWindow.showAtLocation(findViewById(R.id.send),Gravity.CENTER,20,20);//you can remove this effect //Use DropDown way to display popupWindow.showAsDropDown(root); } }
mainActivity的布局文件:
<?xml version="1.0" encoding="utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/idtatabHost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <Button android:id="@+id/send" android:onClick="send" android:text="點(diǎn)我一下 有驚喜(嚇) 。。。" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
/layout/cell.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/cell" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" android:src="@drawable/wechat" android:scaleType="fitXY"/> <Button android:id="@+id/button01" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#ffffffff" android:text="Close" android:textSize="15dp"/> </LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
android實(shí)現(xiàn)支付寶咻一咻的幾種思路方法
本篇文章主要介紹了android實(shí)現(xiàn)支付寶咻一咻的幾種思路方法,詳解的介紹了幾種實(shí)現(xiàn)咻一咻的思路和方法,有需要的可以了解一下。2016-11-11Android編程實(shí)現(xiàn)圖片拍照剪裁的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)圖片拍照剪裁的方法,涉及Android調(diào)用裁剪工具操作圖片的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12Android開(kāi)發(fā)實(shí)現(xiàn)圖片圓角的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)圖片圓角的方法,涉及Android針對(duì)圖形圖像的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10Android Rsa數(shù)據(jù)加解密的介紹與使用示例
RSA是第一個(gè)既能用于數(shù)據(jù)加密也能用于數(shù)字簽名的算法。它易于理解和操作,也很流行。想起自己曾經(jīng)使用過(guò)的Rsa非對(duì)稱(chēng)加密算法,閑下來(lái)總結(jié)一下。方便自己和大家以后使用的時(shí)候參考借鑒。下面來(lái)一起看看吧。2016-09-09Flutter搞定寬高不統(tǒng)一布局開(kāi)發(fā)的方法詳解
我們?cè)陂_(kāi)發(fā)移動(dòng)端界面的時(shí)候,經(jīng)常會(huì)遇到一組尺寸不一的組件需要作為同一組展示,所以本文就將利用Wrap組件搞定寬高不統(tǒng)一布局開(kāi)發(fā),需要的可以參考一下2023-06-06使用Docker來(lái)加速構(gòu)建Android應(yīng)用的基本部署思路解析
這篇文章主要介紹了使用Docker來(lái)加速構(gòu)建Android應(yīng)用的部署思路解析,在服務(wù)器中通過(guò)Docker鏡像來(lái)獲得更高效的開(kāi)發(fā)和測(cè)試流程,需要的朋友可以參考下2016-01-01android Handler詳細(xì)使用方法實(shí)例
本文主要介紹Android中Handler的簡(jiǎn)單使用方法,Handler跟多線程,消息隊(duì)列聯(lián)系很緊密,在平常的實(shí)際程序開(kāi)發(fā)中比較常見(jiàn)。本文分為4個(gè)簡(jiǎn)單的例子來(lái)學(xué)校handler2013-11-11