Popupwindow 显示, 其它背景变暗。 并加上点击事件 ~ (用于记录)
public class MainActivity extends Activity implements OnClickListener {
protected int mScreenWidth;protected int mScreenHeight;
private PopupWindow popupwindow;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
//获取当前屏幕宽高
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
mScreenWidth = metric.widthPixels;
mScreenHeight = metric.heightPixels;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
if (popupwindow != null&&popupwindow.isShowing()) {
popupwindow.dismiss();
return;
} else {
initmPopupWindowView();
popupwindow.showAsDropDown(v, 0, 5);
}
break;
default:
break;
}
}
public void initmPopupWindowView() {
// 获取自己定义布局文件pop.xml的视图
View customView = getLayoutInflater().inflate(R.layout.popview_item,
null, false);
// 创建PopupWindow实例,是宽度和高度
popupwindow = new PopupWindow(customView , mScreenWidth , 600);
popupwindow.setAnimationStyle(R.style.AnimationFade);
WindowManager.LayoutParams params= getWindow().getAttributes();
params.alpha=0.5f;
getWindow().setAttributes(params);
popupwindow.setOutsideTouchable(true);
popupwindow.setFocusable(true);
popupwindow.setTouchable(true);
// popupwindow.setBackgroundDrawable(new BitmapDrawable());
// 自己定义view加入触摸事件
customView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
closePopupWindow();
System.out.println("44444444444444444444");
return false;
}
});
/** 在这里能够实现自己定义视图的功能 */
Button btton2 = (Button) customView.findViewById(R.id.button2);
Button btton3 = (Button) customView.findViewById(R.id.button3);
btton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent2 = new Intent(MainActivity.this,A.class);
startActivity(intent2);
popupwindow.dismiss();
}
});
btton3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent3 = new Intent(MainActivity.this,B.class);
startActivity(intent3);
popupwindow.dismiss();
}
});
}
/**
* 关闭窗体
*/
private void closePopupWindow()
{
if (popupwindow != null && popupwindow.isShowing()) {
popupwindow.dismiss();
popupwindow = null;
WindowManager.LayoutParams params= getWindow().getAttributes();
params.alpha=1.0f;
getWindow().setAttributes(params);
}
}
}
图1:
图2:
布局文件: activity_mian.xml
<LinearLayout
android:id="@+id/a_c"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#C0C0C0"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="点击下拉列表" />
</LinearLayout>
还有一个xml里 。就两个button , 没什么好贴的。
http://download.csdn.net/detail/u012062810/8897455 下载地址
点击popwindow 的时候, ”点击下拉表“ (如图1) 也会变暗。
有解决此方法的小伙伴,希望略微指点下~ 多谢。