PopupWindonw的简单应用

来源:互联网 发布:征服者数据更新 编辑:程序博客网 时间:2024/05/21 17:43

今天跟大家说一下popupWindow的简单应用,这里是入门级的,高手请绕道!
效果图:
这里写图片描述

这里写图片描述

PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。
PopupWindow的使用
其实PopupWindow的使用非常简单,总的来说分为两步:

1、调用PopupWindow的构造器创建PopupWindow对象,并完成一些初始化设置。2、调用PopupWindow的showAsDropDown(View view)将PopupWindow作为View组件的下拉组件显示出来;或调用PopupWindow的showAtLocation()方法将PopupWindow在指定位置显示出来。

创建并完成初始化设置:

PopupWindow popupWindow = new PopupWindow(this);popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);popupWindow.setContentView(LayoutInflater.from(this).inflate(R.layout.layout_popupwindow_style01, null));popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));popupWindow.setOutsideTouchable(false);popupWindow.setFocusable(true);

其中,setWidth、setHeight和setContentView三者必须实现,否则将不会显示任何视图。 setwidth和setHeight的参数值可以是具体的数值,也可以是MATCH_PARENT或者是WRAP_CONTENT。setContentView则是为PopupWindow设置视图内容。 setFocusable顾名思义就是让PopupWindow获得焦点。 setBackgroundDrawable从字面理解就是为PopupWindow设置一个背景。 setOutsideTouchable则表示PopupWindow内容区域外的区域是否响应点击事件,Android官方给出的文档则表示点击内容区域外的区域是否关闭窗口,那么设置为true应该就是表示关闭,设置为false就是表示不关闭咯! 那么我们就动手试一下吧。
看代码

import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.PopupWindow;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private PopupWindow pop;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void popupWindow(View v) {showPopupWindow();// 方法}private void showPopupWindow() {// 布局View v = View.inflate(MainActivity.this, R.layout.pop, null);// 控件Button bt1 = (Button) v.findViewById(R.id.bt1);Button bt2 = (Button) v.findViewById(R.id.bt2);Button bt3 = (Button) v.findViewById(R.id.bt3);// 点击事件bt1.setOnClickListener(this);bt2.setOnClickListener(this);bt3.setOnClickListener(this);// 实例化popupwind,并加载视图v 还有popupwind展示的大小pop = new PopupWindow(v, 200, 150, true);//pop.setTouchable(true);// 设置可被点击// pop.setTouchInterceptor(new OnTouchListener() {//// @Override// public boolean onTouch(View v, MotionEvent event) {// // TODO Auto-generated method stub// System.out.println("mengdd, onTouch ");// return false;// }// });// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框// 我觉得这里是API的一个bugpop.setBackgroundDrawable(getResources().getDrawable(R.drawable.bule));// 设置pop背景颜色pop.showAsDropDown(v, 20, 100);// 展示}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.bt1:pop.dismiss();//关闭popupwindowToast.makeText(MainActivity.this, "bt1", Toast.LENGTH_SHORT).show();// 吐司一下,也可以做点击事件break;case R.id.bt2:Toast.makeText(MainActivity.this, "bt2", Toast.LENGTH_SHORT).show();break;case R.id.bt3:Toast.makeText(MainActivity.this, "bt3", Toast.LENGTH_SHORT).show();break;default:break;}}}

主布局

    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.ebwie.popuwindow.MainActivity" >    </Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="popupWindow"        android:text="点我" />

popupwind布局

    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >     </Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="bt1" />     </Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="bt2" />     </Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="bt3" />

这样一个简单的popupwindows窗口就做好了,赶快去试试吧

原创粉丝点击