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

使用方法:
使用PopupWindow 創(chuàng)建對話框風格的串口秩序如下兩步即可:
1. PopupWindow 的構(gòu)造器創(chuàng)建PopupWindow對象
2. PopupWindow 的showAsDropDown() 將其顯示效果設(shè)置為下拉顯示
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>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android ListView里控件添加監(jiān)聽方法的實例詳解
這篇文章主要介紹了Android ListView里控件添加監(jiān)聽方法的實例詳解的相關(guān)資料,這里提供實例幫助大家學習理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
Android編程實現(xiàn)應(yīng)用強制安裝到手機內(nèi)存的方法
這篇文章主要介紹了Android編程實現(xiàn)應(yīng)用強制安裝到手機內(nèi)存的方法,涉及Android中屬性設(shè)置的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Android Studio 4.0新特性及升級異常問題的解決方案
這篇文章主要介紹了Android Studio 4.0新特性及升級異常的相關(guān)問題,本文給大家分享解決方案,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
android獲取情景模式和鈴聲 實現(xiàn)震動、鈴聲提醒
這篇文章主要介紹了android獲取情景模式和鈴聲,實現(xiàn)震動、鈴聲提醒,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
android ContentResolver獲取手機電話號碼和短信內(nèi)容
這篇文章主要為大家詳細介紹了android ContentResolver獲取手機電話號碼、短信內(nèi)容,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07

