PopupWindow的应用 1
PopupWindow的使用,这里是分析QQ通讯录的源码看到的,这里分享下:
以“拨号盘”界面的-最近联系人 右边的“筛选”按钮为例:
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:orientation="vertical" android:id="@+id/tabcontent"android:layout_width="fill_parent" android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><RelativeLayout android:id="@+id/tv_call_log" android:background="@drawable/bg_header" android:layout_width="fill_parent" android:layout_height="wrap_content"><TextView android:textSize="20.0sp" android:textColor="#ffffff" android:gravity="center" android:id="@+id/titleText"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="最近联系人" android:layout_centerInParent="true" /><com.farmer.views.PopuMenuTextButtonandroid:textColor="#ffffff" android:gravity="center" android:id="@+id/titleImg" android:background="@drawable/bg_top_btn" android:layout_width="46.0dip"android:layout_height="34.0dip" android:layout_marginRight="4.0dip" android:text="筛选" android:layout_alignParentRight="true" android:layout_centerVertical="true" /></RelativeLayout></LinearLayout>
public abstract interface nl{ public abstract void popAwindow(View paramView);}public class PopuMenuTextButton extends Button{ private nl pwindow; public PopuMenuTextButton(Context paramContext, AttributeSet paramAttributeSet) { super(paramContext, paramAttributeSet); } protected void onDetachedFromWindow() { if (this.pwindow != null) this.pwindow.popAwindow(this); super.onDetachedFromWindow(); } public void setOnDetachedListener(nl paramnl) { this.pwindow = paramnl; }}private PopuMenuTextButton pbtn;private PopupWindow pwindow; //onCreate.......protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.layout_dialerpad);this.pbtn = (PopuMenuTextButton) findViewById(R.id.titleImg);pbtn.setOnClickListener(this);} //显示PopupWindow 窗口 public void showPop(){int i1 = -1; if (upOrdown() == i1){ //这边是需要再构造一个布局文件的 View localView1 = LayoutInflater.from(this).inflate( R.layout.layout_popup_calllog_type, null); View localView2 = localView1.findViewById(R.id.popup_callLog_all); View localView3 = localView1.findViewById(R.id.popup_callLog_miss); View localView4 = localView1.findViewById(R.id.popup_callLog_incoming); View localView5 = localView1.findViewById(R.id.popup_callLog_outgoing); localView2.setOnClickListener(this); localView3.setOnClickListener(this); localView4.setOnClickListener(this); localView5.setOnClickListener(this); PopupWindow localPopupWindow1 = new PopupWindow(localView1, i1, i1); this.pwindow = localPopupWindow1; this.pwindow.showAsDropDown(this.pbtn); }}public int upOrdown() {int i1 = -1;if ((this.pwindow != null) && (this.pwindow.isShowing())) {this.pwindow.dismiss();i1 = 1;}return i1;}@Overridepublic void popAwindow(View paramView) {upOrdown();}