我用gridview实现menu功能,然后用popupWindow弹出menu,当我点击menu时,实现点击事件,同时我想隐藏menu,但现在问题是隐藏了menu,在我再按menu键时,menu却不弹出来了,求大能帮帮忙,下面贴代码: public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub LayoutInflater inflater=(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.main, null);
grid1=(GridView) view.findViewById(R.id.menuGridChange);
grid1.setAdapter(new ImageAdapter(this));
//用PopupWindow弹出menu
pw=new PopupWindow(view,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
pw.showAtLocation(findViewById(R.id.menuGridChange), Gravity.CENTER, 0, 300);
return super.onCreateOptionsMenu(menu);
}
我用的是自定义适配器,
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//用代码实现
LinearLayout linear=new LinearLayout(context);
LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
linear.setOrientation(LinearLayout.VERTICAL);
ImageView iv=new ImageView(context);
iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(resArray[position])).getBitmap());
LayoutParams param2=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
param2.gravity=Gravity.CENTER;
linear.addView(iv,param2);
TextView tv=new TextView(context);
tv.setText(title[position]);
LayoutParams params3=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params3.gravity=Gravity.CENTER;
linear.addView(tv,params3);
linear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (position) {
case 0:
Toast.makeText(MenuBgActivity.this, "this is the first!", Toast.LENGTH_SHORT).show();
pw.dismiss();
break;
case 1:
Toast.makeText(MenuBgActivity.this, "this is the second!", Toast.LENGTH_SHORT).show();
break;
}
}
});
return linear;
}
// TODO Auto-generated method stub LayoutInflater inflater=(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.main, null);
grid1=(GridView) view.findViewById(R.id.menuGridChange);
grid1.setAdapter(new ImageAdapter(this));
//用PopupWindow弹出menu
pw=new PopupWindow(view,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
pw.showAtLocation(findViewById(R.id.menuGridChange), Gravity.CENTER, 0, 300);
return super.onCreateOptionsMenu(menu);
}
我用的是自定义适配器,
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//用代码实现
LinearLayout linear=new LinearLayout(context);
LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
linear.setOrientation(LinearLayout.VERTICAL);
ImageView iv=new ImageView(context);
iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(resArray[position])).getBitmap());
LayoutParams param2=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
param2.gravity=Gravity.CENTER;
linear.addView(iv,param2);
TextView tv=new TextView(context);
tv.setText(title[position]);
LayoutParams params3=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params3.gravity=Gravity.CENTER;
linear.addView(tv,params3);
linear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (position) {
case 0:
Toast.makeText(MenuBgActivity.this, "this is the first!", Toast.LENGTH_SHORT).show();
pw.dismiss();
break;
case 1:
Toast.makeText(MenuBgActivity.this, "this is the second!", Toast.LENGTH_SHORT).show();
break;
}
}
});
return linear;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货