android典型程式碼系列(二十五)------popupwindow的使用

fandong12388發表於2015-12-06

popupwindow的使用 :

public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
/************************** 螢幕適配 ************************************/
        int popupWindowWidth = Integer.valueOf(getResources().getString(R.string.popwindowwidth).toString().trim());
        int popupWindowHeight = Integer.valueOf(getResources().getString(R.string.popwindowheight).toString().trim());
        int dx = Integer.valueOf(getResources().getString(R.string.popwindowdx).toString().trim());
        /**********************************************************************/
        int[] location = new int[2];
        view.getLocationInWindow(location);
        int x = location[0] + dx;
        int y = location[1];
        AppInfo info = (AppInfo) lv_AppManagerAcitivty.getItemAtPosition(position);
        View popupWindow = View.inflate(this, R.layout.popwindow, null);
        ll_start = (LinearLayout) popupWindow.findViewById(R.id.ll_start);
        ll_uninstall = (LinearLayout) popupWindow.findViewById(R.id.ll_uninstall);
        ll_share = (LinearLayout) popupWindow.findViewById(R.id.ll_share);
        ll_start.setTag(position);
        ll_uninstall.setTag(position);
        ll_share.setTag(position);

        ll_start.setOnClickListener(this);
        ll_uninstall.setOnClickListener(this);
        ll_share.setOnClickListener(this);

        localPopupWindow = new PopupWindow(popupWindow, popupWindowWidth,popupWindowHeight);
        Drawable background = getResources().getDrawable(R.drawable.local_popup_bg);
//注意:popwindow一定要設定背景   
localPopupWindow.setBackgroundDrawable(background);
        localPopupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);
}

相關文章