1 效果图
2 知识点
PopupWindow(View contentView, int width, int height) //创建一个没有获取焦点、长为width、宽为height,内容为cntentView的popup window.
PopupWindow(View contentView, int width, int height, boolean focusable) //类似上面那个,但第四个参数可以控制是否获取焦点 //(这2种使用较多)!!!!
3 弹出框的界面
layout_about_popup.xml
<?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="wrap_content" android:background="@color/bg_about_popup" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:orientation="horizontal" > <LinearLayout android:id="@+id/ll_share_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:background="@drawable/button_share_1_pressed" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="5dp" android:text="微信好友" android:textSize="13sp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <LinearLayout android:id="@+id/ll_share_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:background="@drawable/button_share_4_pressed" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="5dp" android:text="微信朋友圈" android:textSize="13sp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <LinearLayout android:id="@+id/ll_share_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:background="@drawable/button_share_2_pressed" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="5dp" android:text="QQ登录" android:textSize="13sp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <LinearLayout android:id="@+id/ll_share_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:background="@drawable/button_share_3_pressed" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="5dp" android:text="微博登录" android:textSize="13sp" /> </LinearLayout> </LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginBottom="5dp" android:background="@android:color/black" /> <Button android:id="@+id/btn_share_call_off" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:background="@color/transparent" android:text="取消" /> </LinearLayout>
4 button_share_1_pressed.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/login_share_2_0" android:state_pressed="false"></item> <item android:drawable="@drawable/login_share_2_1" android:state_pressed="true"></item> </selector>
5 .java
private LinearLayout layout_weixin; private LinearLayout layout_weixin_friends; private LinearLayout layout_qq; private LinearLayout layout_sina; private Button btn_out_off; private View view; private PopupWindow pop;
/***
* 获取PopupWindow实例
*/
private void getPopupWindow() {
if (null != pop) {
closePopupWindow();
return;
} else {
initPopuptWindow();
}
}
/**
* 创建PopupWindow
*/
protected void initPopuptWindow() {
// PopupWindow实例化
pop = new PopupWindow(view, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, true);
pop.setAnimationStyle(R.style.MenuAnimationFade);
// 弹出窗口显示内容视图,默认以锚定视图的左下角为起点,这里为点击按钮
pop.showAtLocation(tv_about_version, Gravity.BOTTOM, 0, 0);
WindowManager.LayoutParams params = this.getWindow().getAttributes();
params.alpha = 0.5f;
this.getWindow().setAttributes(params);
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
closePopupWindow();
return false;
}
});
}
@Override public void onClick(View v) { switch (v.getId()) { case R.id.ic_about_back: finish(); break; case R.id.ic_about_help: Uri uri = Uri.parse("http://www.etoury.com/help/app.html"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); break; case R.id.btn_about_share: getPopupWindow(); break; case R.id.ll_share_1: finish(); break; case R.id.ll_share_2: finish(); break; case R.id.ll_share_3: finish(); break; case R.id.ll_share_4: finish(); break; case R.id.btn_share_call_off: closePopupWindow(); break; default: break; } }
/**
* 关闭窗口
*/
private void closePopupWindow() {
if (pop != null && pop.isShowing()) {
pop.dismiss();
pop = null;
WindowManager.LayoutParams params = this.getWindow()
.getAttributes();
params.alpha = 1f;
this.getWindow().setAttributes(params);
}
}