Android 标题栏菜单设置与应用(popupWindow的应用)

效果图
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKEAAACfCAIAAAAeQy48AAADmklEQVR4nO3cS3aiQBhA4d5Vam0sRSduwszcBptwBwGxB3aMTfHK4wfr5t7DJBxzCv1CSRD8czV6f7beAAvvn3GTkgt1+TDe7u/s6xW62SvHMR78E+49+HH92F/92s8hPo7x8gfn9j+5Tc/X7zLOd9ZCn/inKtL4axNsvh//hon6WqjxvQmtwV22t/KXTNoFG4/tfMufy/3g67O/uELd62t3PH5//RVgfF2wO47t309r3B2Pt43ssX12/a1SjXOkHtXYM8oPuDR+0nLIwUOqJb/yhMZX5+ppzt6BWG/92LFY9DZvWJHG92bfjyf22t4DCn0FlkQwnliZv0lPnAAp9EWYDW6cr8TPzHko47Ej6oX/TVH5UcaDa6xsY1uS14HwF6/n4qcxP42j2nZ+ftwSjaNqtjuM1XilNOanMT+N+WnMT2N+GvPTmJ/G/CaNz5cqtafzOkNrHNWUcX1oksblN2x8PrXvp5Q1Lr7JubpuNQakMT+N+WnMT2N+GvPTmJ/G/GbPgcSdCdF4pfxMgp/G/JqU2v1+k+UtpXa/v2+JxlE1KbW73SbL28tLu9vdt0TjqJyr+WnMT2N+GvMbNO4O/58AqdcYWuOohozPl+rQvf/QnaogZo1XasFcXbcpNYefR9Z4pRYYny9VaqpTN/e4bw6tcVTzxrfPJ9yPy23W+Hb85ftxwc0Y14cmZifOh9Y4qinj2ywd8E48OLTGUY0b121KTToEAedDaxzViHEdugcPDq1xVEPGawDnQ2sc1YBxdjFX0BlNjVfKzyT4acxPY373F3qra/bul+1pHNWH8UbX7N0v29M4Kudqfhrz05ifxvw05qcxP435acxPY34a88uN+zfC+J0vpZcZ1+3jNVyRX2+s8UrNzdVRN0nkQ2sclcb8po27U9Wk6hLzjqzxSo0Yny9VirtDYnBojaNaci+M95iX3aJ72nw/Lrp541MVdEeMxivlfsyvb1wfHkXjvgwkH1rjqIbOcz2ey4z6xykfWuOo/EyCn8b8NOanMT+N+WnMT2N+GvPTmF/+3S5rLo9bojE/jflpzE9jfhrz05ifxvw05qcxP435acxPY34a89OYn8b8NOanMT+N+WnMT2N+GvPTmJ/G/DTmpzE/jflpzE9jfhrz05ifxvw05qcxP435acxPY34a89OYn8b8NOanMT+N+WnMT2N+GvPTmJ/G/DTmpzE/jflpzE9jfhrz05ifxvw05qcxv79qGFcrDcv7+AAAAABJRU5ErkJggg==" alt="" />
1、布局文件 layout_popup.xml
<ListView android:id="@+id/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@android:color/transparent"/>
layout_popup_list.xml

<LinearLayout
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="@dimen/space_40"
android:background="@drawable/block_item_default_selector">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="@dimen/font_main_size"
android:textColor="@color/font_main_color"/>
</LinearLayout> 2.java文件
private void showPopupWindow(View view) {

    // 一个自定义的布局,作为显示的内容
View contentView = LayoutInflater.from(this).inflate(
R.layout.layout_popup, null); //状态选择
String status = "yespwd";
ListView popuplist = (ListView)contentView.findViewById(R.id.popup);
data = getData(status);
SimpleAdapter simpleAdapter = new SimpleAdapter(this,data,
R.layout.layout_popup_list, new String[] {"text"},
new int[] {R.id.text});
popuplist.setAdapter(simpleAdapter);
final PopupWindow popupWindow = new PopupWindow(contentView,
200, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//点击外部后消失
popupWindow.setTouchable(true);
popuplist.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String text = (String) data.get(i).get("text");
if (text.equals("status")) {
Intent intent = new Intent(PrePaymentActivity.this, PaymentHistoryActivity.class);
startActivity(intent);
         //弹框消失
         popupWindow.dismiss();
}
}
});
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
}
}); // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.color.white));
// 设置好参数之后再show popupWindow.showAsDropDown(view);
} //输入数据
private List<Map<String, Object>> getData(String stutas)
{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map,map1,map2,map3;
if(stutas.equals("yespwd")){
map = new HashMap<String, Object>();
map.put("text", "1");
map1 = new HashMap<String, Object>();
map1.put("text", "2");
map2 = new HashMap<String, Object>();
map2.put("text", "3"); list.add(map);
list.add(map1);
list.add(map2);
} return list;
}
 
上一篇:解决walle报错:宿主机代码检出检测出错,请确认svn用户名密码无误


下一篇:.Net中C#的DllImport的用法