你的位置:首页 > 操作系统

[操作系统]PopupWindow


 
1 效果图
QQ图片20151206100403
QQ图片20151206100408
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.
<??><LinearLayout ="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.
<??><selector ="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;

// 引入窗口配置文件 - 即弹窗的界面    view = inflater.inflate(R.layout.layout_about_popup, null);    layout_weixin = (LinearLayout) view.findViewById(R.id.ll_share_1);    layout_weixin_friends = (LinearLayout) view        .findViewById(R.id.ll_share_2);    layout_qq = (LinearLayout) view.findViewById(R.id.ll_share_3);    layout_sina = (LinearLayout) view.findViewById(R.id.ll_share_4);    btn_out_off = (Button) view.findViewById(R.id.btn_share_call_off);

// PopupWindow实例化    pop = new PopupWindow(view, LayoutParams.MATCH_PARENT,        LayoutParams.WRAP_CONTENT, true);    pop.setAnimationStyle(R.style.MenuAnimationFade);

 

@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:      /* share(); */      changePopupWindowState();      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:      changePopupWindowState();      break;    default:      break;    }  }

/**   * 改变 PopupWindow 的显示和隐藏   */  private void changePopupWindowState() {    if (pop.isShowing()) {      // 隐藏窗口,如果设置了点击窗口外消失,则不需要此方式隐藏      pop.dismiss();    } else {      // 弹出窗口显示内容视图,默认以锚定视图的左下角为起点,这里为点击按钮      pop.showAtLocation(tv_about_version, Gravity.BOTTOM, 0, 0);    }  }