android实现弹出窗口的方式有两种,第一种用Activity实现弹出窗口的效果,第二种用PopupWindow组件实现
1,用activity方式实现
public class DialogActivity extends Activity implements OnClickListener {
private LinearLayout commentView;
private LinearLayout shareView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_youji);
commentView = (LinearLayout) findViewById(R.id.comment_div);
shareView = (LinearLayout) findViewById(R.id.share_div);
commentView.setOnClickListener(this);
shareView.setOnClickListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
finish();
return true;
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.comment_div:
Toast.makeText(getApplicationContext(), "comment",
Toast.LENGTH_SHORT).show();
break;
case R.id.share_div:
Toast.makeText(getApplicationContext(), "collect",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dialog" android:orientation="horizontal" > <LinearLayout android:id="@+id/comment_div" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="vertical" android:padding="24dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pl" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="评论" android:textColor="#000000" /> </LinearLayout> <LinearLayout android:id="@+id/share_div" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="vertical" android:padding="24dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/share" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="转发" android:textColor="#000000" /> </LinearLayout></LinearLayout>
AndroidManifest.xml中
<activitystyles.xml中
android:name="com.xx.controller.dialog.UpdateDialogActivity"
android:theme="@style/MyDialogStyle" >
</activity>
<style name="MyDialogStyle">
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:windowFrame">@null</item>
<!-- 边框 -->
<item name="android:windowNoTitle">true</item>
<!-- 无标题 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 半透明 -->
<item name="android:windowContentOverlay">@null</item>
<!-- 内容覆盖 -->
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<!-- 窗口样式Dialog -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 模糊 -->
</style>
2,用PopupWindow方式实现
private void showPopupWindow() {popmenu.xml
if (popupWindow == null) {
popupContentView = (LinearLayout) LayoutInflater.from(
DetailActivity.this).inflate(R.layout.popmenu, null);
menumaindiv = (LinearLayout) popupContentView
.findViewById(R.id.menumaindiv);
popupWindow = new PopupWindow(DetailActivity.this);
// 设置半透明灰色
ColorDrawable dw = new ColorDrawable(0xb0000000);
popupWindow.setBackgroundDrawable(dw);
// popupWindow.setBackgroundDrawable(new BitmapDrawable()); //
// 必须设置,否则获得焦点后页面上其他地方点击无响应
popupWindow.setFocusable(true); //
// 设置PopupWindow可获得焦点,必须设置,否则listView无法响应
popupWindow.setTouchable(true); // 设置PopupWindow可触摸
popupWindow.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
// ,点击其他地方 popupWindow消失
popupWindow.setClippingEnabled(true);
popupWindow.setContentView(popupContentView);
popupWindow.setWidth(LayoutParams.MATCH_PARENT);
popupWindow.setHeight(LayoutParams.MATCH_PARENT);
popupWindow.setAnimationStyle(R.style.popuStyle); // 设置
// popupWindow动画样式
}
popupWindow.showAtLocation(createView, Gravity.CENTER, 10, 10);
popupWindow.update();
// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
popupContentView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int topheight = menumaindiv.getTop();
int bottomheight = menumaindiv.getBottom();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if ((y >= 0 && y < topheight) || (y > bottomheight)) {
popupWindow.dismiss();
}
}
return true;
}
});
}
<?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" >
<LinearLayout
android:id="@+id/menumaindiv"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/dialog"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/comment_div"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pl" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="评论"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="@+id/share_div"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/share" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="转发"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
1.PopupWindow的layout_width、layout_height都要设置为LayoutParams.MATCH_PARENT
2.popmenu.xml的布局外层需要多加一层LinearLayout,这样可以保证内部的LinearLayout可以自定义宽高
3.PopupWindow全屏后popupWindow.setOutsideTouchable(true); 就没意义了,这时候需要给窗口内部的组件popupContentView定义setOnTouchListener事件
这里根据点击的位置进行popupWindow.dismiss();的操作