第一步:在那个id中
case R.id.supmart_img: if (v == supmart_img) { if (isChanged) { supmart_img.setImageDrawable(getResources().getDrawable(R.mipmap.yuan)); } else { showPopUp(v); supmart_img.setImageDrawable(getResources().getDrawable(R.mipmap.seyuan)); } isChanged = !isChanged; } break;
boolean isChanged = false;
private void showPopUp(View parent) { view2 = LayoutInflater.from(SupmartActivity.this).inflate(R.layout.popu2, null); popupWindow = new PopupWindow(view2, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); // 获取popwindow焦点 popupWindow.setFocusable(true); // 点击PopupWindow区域外部消失 popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable()); // 设置popwindow出现和消失动画 // popupWindow.setAnimationStyle(R.style.PopMenuAnimation); // 获取点击按钮的坐标 int[] location = new int[2]; supmart_img.getLocationOnScreen(location); // 设置popwindow显示位置 popupWindow.showAtLocation(supmart_img, Gravity.NO_GRAVITY, location[0], location[1] - popupWindow.getHeight()); popupWindow.showAsDropDown(supmart_img); }
第二步:popup2布局中
<LinearLayout android:background="@color/colorPrimary" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <Button android:background="#0000" android:id="@+id/xiaoxingquan" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15px" android:button="@null" android:layout_weight="1" android:text="水果" android:textColor="#cd333333" /> <Button android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15px" android:button="@null" android:background="#0000" android:text="香烟" android:textColor="#cd333333" /> <Button android:background="#0000" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15px" android:button="@null" android:layout_weight="1" android:text="酒水" android:textColor="#cd333333" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15px" android:button="@null" android:layout_weight="1" android:text="蔬菜" android:background="#0000" android:textColor="#cd333333" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15px" android:button="@null" android:layout_weight="1" android:text="冷饮" android:background="#0000" android:textColor="#cd333333" /> </LinearLayout>