Android 中Popwindow弹出菜单的两种方法实例
Android 中Popwindow弹出菜单的两种方法实例
1.popWindow就是对话框的一种方式!
此文讲解的android中对话框的一种使用方式,它叫popWindow。
2、popWindow的特性
Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:
AlertDialog的位置固定,而PopupWindow的位置可以随意。
AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的。
PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。
实例代码:
方法一的Activity
package com.app.test02; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.PopupWindow; import android.widget.Toast; public class PopwindowLeft extends Activity { // 声明PopupWindow对象的引用 private PopupWindow popupWindow; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popupwindow_main); // 点击按钮弹出菜单 Button pop = (Button) findViewById(R.id.popBtn); pop.setOnClickListener(popClick); } // 点击弹出左侧菜单的显示方式 OnClickListener popClick = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub getPopupWindow(); // 这里是位置显示方式,在屏幕的左侧 popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0); } }; /** * 创建PopupWindow */ protected void initPopuptWindow() { // TODO Auto-generated method stub // 获取自定义布局文件activity_popupwindow_left.xml的视图 View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null, false); // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度 popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true); // 设置动画效果 popupWindow.setAnimationStyle(R.style.AnimationFade); // 点击其他地方消失 popupWindow_view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); popupWindow = null; } return false; } }); } /*** * 获取PopupWindow实例 */ private void getPopupWindow() { if (null != popupWindow) { popupWindow.dismiss(); return; } else { initPopuptWindow(); } } }
方法二的Activity
package com.app.test02; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup.LayoutParams; import android.widget.PopupWindow; public class PopwindowLeftNew extends Activity{ private PopupWindow popupWindow; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_popupwindow_main); findViewById(R.id.popBtn).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 获取自定义布局文件activity_popupwindow_left.xml的视图 View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,false); // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度 popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true); // 设置动画效果 popupWindow.setAnimationStyle(R.style.AnimationFade); // 这里是位置显示方式,在屏幕的左侧 popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0); // 点击其他地方消失 popupWindow_view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); popupWindow = null; } return false; } }); } }); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
赞 (0)