在项目中我们经常会用单PopupWindonw弹框,今天就简单的介绍一下哦
不做gif图了,效果图如下:
PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。
PopupWindow的简单使用
- 调用PopupWindow的构造器创建PopupWindow对象,并完成一些初始化设置。
- 调用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;
@Override
protected 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的一个bug
pop.setBackgroundDrawable(getResources().getDrawable(R.drawable.bule));// 设置pop背景颜色
pop.showAsDropDown(v, 20, 100);// 展示
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt1:
pop.dismiss();//关闭popupwindow
Toast.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" />
就是这样简单,赶快去试试吧