Android開發(fā)之PopupWindow創(chuàng)建彈窗、對話框的方法詳解
本文實例講述了Android開發(fā)之PopupWindow創(chuàng)建彈窗、對話框的方法。分享給大家供大家參考,具體如下:
簡介:
PopupWindow 可創(chuàng)建類似對話框風格的窗口
效果:

使用方法:
使用PopupWindow 創(chuàng)建對話框風格的串口秩序如下兩步即可:
1. PopupWindow 的構造器創(chuàng)建PopupWindow對象
2. PopupWindow 的showAsDropDown() 將其顯示效果設置為下拉顯示
3. PopupWindow 的showAtLoacation() 方法將PopupWindow() 在指定位置顯示出來
下拉顯示效果:

具體實現(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="點我一下 有驚喜(嚇) 。。。"
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>
更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android Rsa數(shù)據(jù)加解密的介紹與使用示例
RSA是第一個既能用于數(shù)據(jù)加密也能用于數(shù)字簽名的算法。它易于理解和操作,也很流行。想起自己曾經(jīng)使用過的Rsa非對稱加密算法,閑下來總結一下。方便自己和大家以后使用的時候參考借鑒。下面來一起看看吧。2016-09-09
Flutter搞定寬高不統(tǒng)一布局開發(fā)的方法詳解
我們在開發(fā)移動端界面的時候,經(jīng)常會遇到一組尺寸不一的組件需要作為同一組展示,所以本文就將利用Wrap組件搞定寬高不統(tǒng)一布局開發(fā),需要的可以參考一下2023-06-06
使用Docker來加速構建Android應用的基本部署思路解析
這篇文章主要介紹了使用Docker來加速構建Android應用的部署思路解析,在服務器中通過Docker鏡像來獲得更高效的開發(fā)和測試流程,需要的朋友可以參考下2016-01-01

