Android PopupWindow的使用和分析
2015-12-14 09:24
501 查看
PopupWindow使用
PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。PopupWindow使用Demo
这个类的使用,不再过多解释,直接上代码吧。比如弹出框的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFBBFFBB" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Hello My Window" android:textSize="20sp" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Button" android:textSize="20sp" /> </LinearLayout>
弹出框布局
Activity的布局中只有一个按钮,按下后会弹出框,Activity代码如下:
package com.example.hellopopupwindow; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; 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 MainActivity extends Activity { private Context mContext = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showPopupWindow(view); } }); } private void showPopupWindow(View view) { // 一个自定义的布局,作为显示的内容 View contentView = LayoutInflater.from(mContext).inflate( R.layout.pop_window, null); // 设置按钮的点击事件 Button button = (Button) contentView.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "button is pressed", Toast.LENGTH_SHORT).show(); } }); final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); popupWindow.setTouchInterceptor(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.i("mengdd", "onTouch : "); return false; // 这里如果返回true的话,touch事件将被拦截 // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss } }); // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框 // 我觉得这里是API的一个bug popupWindow.setBackgroundDrawable(getResources().getDrawable( R.drawable.selectmenu_bg_downward)); // 设置好参数之后再show popupWindow.showAsDropDown(view); } }
相关文章推荐
- android-性能优化之集合类优化
- Android 屏幕适配方案
- 简阅客户端应用Android源码
- 关于Slidingmenu的若干问题
- android hapticFeedbackEnabled在程序中使用触力反馈
- Android Audio控制和MediaButton远程控制(音视频控制配合)
- 关于ActionBar的几个小细节的解决方案
- Android studio使用
- android 使用AIDL和服务通信实现IPC通信
- Android 中ContentProvider的用法及用ContentProvider访问联系人
- 消息推送之百度云推送Android集成与使用方法
- android日历控件(一)
- Android基础常用日期操作工具类
- 安卓动画
- Android中Paint画笔类的使用详解
- Android动态显示和隐藏状态栏
- android gradle: how to import a library module
- android 转义系列
- Android-Universal-Image-Loader
- Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端