本文实现
1. 实现PopupWindow的简单示例
2. 通过Code判断能否在PopupWindow上在创建一个PopupWindow
先上效果图
分别是 --- Activity --- PopupWindow1 ----PopupWindow2
PopupWindow是阻塞对话框,只有在外部线程 或者 PopupWindow本身做退出操作才行。
PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该会与AlertDialog常混用。
实现这个Code 我发现, 如上所说 , 你想在btn2上调用Activity.this.finish()方法试图销毁当前Activity是无效,并且按Back也是无效的 ~
main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout"> <Button android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#cccccc" > <TextView android:text="@string/hello_world" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_dismiss" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="dismiss"/> <TextView android:text="@string/hello_world" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/add" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="在PopupWindow AddButton 显示PopupWindow" /> </LinearLayout>
<?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="match_parent" android:orientation="vertical" > <TextView android:id="@+id/username_view" android:text="用户名" android:textAppearance="@android:attr/textAppearanceMedium" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:id="@+id/username_edit" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:inputType="none" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/password_view" android:text="密码" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:id="@+id/password_edit" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:inputType="none" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn1" android:layout_weight="1" android:text="btn1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/btn2" android:layout_weight="1" android:text="btn2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
/**
* PopupWindow :
* 1 . 阻塞对话框 ,只有在外部线程 或者 PopupWindow本身做退出操作才行{}
* 2 . 必须依赖Layout布局做外观
* */
public class MainActivity extends Activity {
private boolean flag = false;
PopupWindow popupWindow;
PopupWindow pop2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preInitUI();
initUI();
}
private void preInitUI(){
setContentView(R.layout.main);
Button show = (Button)findViewById(R.id.show);
show.setOnClickListener(listener);
}
private void initUI(){
/**
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
自定义PopupWindow布局 可以在面动态的添加控件* */
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup, null);
popupWindow = new PopupWindow(layout, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Button btn_dismiss = (Button) layout.findViewById(R.id.btn_dismiss);
btn_dismiss.setOnClickListener(listener);
Button add = (Button)layout.findViewById(R.id.add);
add.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_dismiss: openMenu();
break;
case R.id.show: openMenu();
break;
case R.id.add:create();
break;
case R.id.btn1: dis(popupWindow);
break;
case R.id.btn2:MainActivity.this.finish();break;
}
}
};
private void openMenu(){
if(!flag)
{
//overridePendingTransition(R.anim.in, R.anim.out);
popupWindow.setAnimationStyle(R.style.AppTheme);//设置style样式
popupWindow.showAtLocation((findViewById(R.id.show)), Gravity.BOTTOM, 0, 0);//显示PopupWindow
popupWindow.setFocusable(true);//window上面本身要响应点击事件,设置了属性setFocusable(true)
popupWindow.update();
flag = true;
}else{
popupWindow.dismiss();
popupWindow.setFocusable(false);
flag = false;
}
}
// Create new PopupWindow 在PopupWindow上再建一个PopupWindow
private void create()
{
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup2, null);
Button exit = (Button)layout.findViewById(R.id.btn1);
exit.setOnClickListener(listener);
pop2 = new PopupWindow(layout, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
pop2.setAnimationStyle(R.style.AppTheme);
// 显示 popupWindow
pop2.showAtLocation((findViewById(R.id.show)), Gravity.BOTTOM, 0, 0);
}
private void dis(PopupWindow popupwindow)
{
pop2.dismiss();
}
}