android下面popwindow的使用
PopupWindow是阻塞对话框,只有在外部线程或者 PopupWindow本身做退出操作才行。PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该会与AlertDialog常混用。
1.使用 popwindow实现如下效果:
2.代码如下:MainActivity.java
publicclass MainActivity extends Activity {
private Button button;
@Override
publicvoid onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//新建一个按钮
button = (Button) findViewById(R.id.button);
//按钮的监听事件
button.setOnClickListener(new OnClickListener()
{
publicvoid onClick(View v)
{
// 新建一个popwindow,并显示里面的内容
PopupWindow popupWindow = makePopupWindow(MainActivity.this);
//int[] xy = new int[2];
//button.getLocationOnScreen(xy);
//popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,xy[0]/2,xy[1]+button.getWidth());
//popwindow与按钮之间的相对位置
popupWindow.showAsDropDown(button,2, 5);
}
});
}
// 创建一个包含自定义view的PopupWindow
private PopupWindow makePopupWindow(Context cx)
{
PopupWindow window;
window = new PopupWindow(cx);
/*ListView listview = new ListView(this);
listview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
listview.setAdapter(new ArrayAdapter<String>(cx, android.R.layout.simple_expandable_list_item_1,getData()));
*/
//View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow, null);
//window.setContentView(contentView);
Button b1 = new Button(this);
b1.setText("first");
b1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
Button b2 = new Button(this);
b2.setText("Second");
b2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.addView(b1);
linearLayout.addView(b2);
//linearLayout.addView(listview);
linearLayout.setOrientation(LinearLayout.VERTICAL);
window.setContentView(linearLayout); //选择布局方式
//设置popwindow的背景图片
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_pop_pressed));
//设置popwindow的高和宽
window.setWidth(70);
window.setHeight(320);
// 设置PopupWindow外部区域是否可触摸
window.setFocusable(true); //设置PopupWindow可获得焦点
window.setTouchable(true); //设置PopupWindow可触摸
window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
return window;
}
//构造数组的函数
private List<String> getData(){
List<String> data = new ArrayList<String>();
data.add("数学");
data.add("英语");
data.add("语文");
data.add("历史");
data.add("政治");
//data.add("美术");
//data.add("体育");
return data;
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
}
package mtpop.window.main;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
public class MainActivity extends Activity
{
private static final String TAG = "MainActivity";
private Button button;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// 显示 popupWindow
PopupWindow popupWindow = makePopupWindow(MainActivity.this);
int[] xy = new int[2];
button.getLocationOnScreen(xy);
popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,-xy[0]/2,xy[1]+button.getWidth());
//popupWindow.showAsDropDown(button,0, 0);
}
});
}
// 创建一个包含自定义view的PopupWindow
private PopupWindow makePopupWindow(Context cx)
{
PopupWindow window;
window = new PopupWindow(cx);
//View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow, null);
//window.setContentView(contentView);
Button b1 = new Button(this);
b1.setText("first");
b1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
Button b2 = new Button(this);
b2.setText("Second");
b2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.addView(b1);
linearLayout.addView(b2);
linearLayout.setOrientation(LinearLayout.VERTICAL);
window.setContentView(linearLayout);
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.pop_bg));
window.setWidth(DisplayManager.dipToPixel(150));
window.setHeight(DisplayManager.dipToPixel(150));
// 设置PopupWindow外部区域是否可触摸
window.setFocusable(true); //设置PopupWindow可获得焦点
window.setTouchable(true); //设置PopupWindow可触摸
window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
return window;
}
}
3. main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Title" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="click" />
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Title" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click" />
</LinearLayout>
</LinearLayout>
注意:
* 新建一个popupWindow弹出框 popupWindow是一个阻塞式的弹出框,这就意味着在我们退出这个弹出框之前,程序会一直等待,
* 这和AlertDialog不同哦,AlertDialog是非阻塞式弹出框,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。 *
******************************************************************
1.PopupWindow的隐藏
// 隐藏菜单
publicvoid dismiss() {
Window.dismiss();
}final PopupWindow window = mPageStatWin;
if(null != window && window.isShowing()) {
win.dismiss();
}
2.Popupwindow的显示及位置设置
window.showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10);
第一个参数指定PopupWindow的锚点view,即依附在哪个view上。
第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。
//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
window.showAsDropDown(anchor);
//xoff,yoff基于anchor的左下角进行偏移。
window.showAsDropDown(anchor, xoff, yoff);
如果没有充足的空间显示PopupWindow,那么PopupWindow的左下角将位于anchor的左上角来显示。
* popupWindow.showAsDropDown(View view)弹出对话框,位置在紧挨着view组件
* showAsDropDown(View anchor, int xoff, int yoff)弹出对话框,位置在紧挨着view组件,x y 代表着偏移量
* showAtLocation(View parent, int gravity, int x, int y)弹出对话框
* parent 父布局 gravity 依靠父布局的位置如Gravity.CENTER x y 坐标值
//设置popwindow的高和宽
window.setWidth(70);
window.setHeight(320);
// 设置PopupWindow外部区域是否可触摸
window.setFocusable(true); //设置PopupWindow可获得焦点
window.setTouchable(true); //设置PopupWindow可触摸
window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
设置其可以获得焦点,可触摸,外部区域可触摸(很重要)