本文實例講述了Android開發之PopupWindow創建彈窗、對話框的方法。分享給大家供大家參考,具體如下:
簡介:
PopupWindow 可創建類似對話框風格的窗口
效果:
使用方法:
使用PopupWindow 創建對話框風格的串口秩序如下兩步即可:
1. PopupWindow 的構造器創建PopupWindow對象
2. PopupWindow 的showAsDropDown()
將其顯示效果設置為下拉顯示
3. PopupWindow 的showAtLoacation()
方法將PopupWindow()
在指定位置顯示出來
下拉顯示效果:
具體實現方法:
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相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!