点击主界面 PopupMenu消失

   我们有时想做出弹出PopupMenu,主界面变暗,点击主界面,PopupMenu消失。主代码如下


package com.example.mypopupwindow;


import java.util.ArrayList;
import java.util.List;


import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListPopupWindow;


@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener, OnDismiss {


private List<String> titles;
private List<List<String>> item_names; 
private List<List<Integer>> item_images; 
private ListPopupWindow myPopupMenu;
ImageView bg;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

titles = new ArrayList<String>();
titles = addItems(new String[]{"menu"});


item_images = new ArrayList<List<Integer>>();
item_images.add(addItems(new Integer[] { R.drawable.ic_action_call,
R.drawable.ic_action_camera, R.drawable.ic_action_copy,
R.drawable.ic_action_crop}));

item_names = new ArrayList<List<String>>();
item_names.add(addItems(new String[] { "aaaaaaaaa", "bbbbbbbb", "ccccccc", "ddddddd", "eeeeeeee",
"fffff", "ggggggg", "hhhhhhh" }));
myPopupMenu = new ListPopupWindow(this);
Resources res = this.getResources();
myPopupMenu.setBackgroundDrawable(res.getDrawable(R.drawable.menu_bg));
myPopupMenu.setWidth(600);
myPopupMenu.setHeight(LayoutParams.WRAP_CONTENT);

   BodyAdapter bodyAdapter = new BodyAdapter(this, item_names.get(0),item_images.get(0));
   myPopupMenu.setAdapter(bodyAdapter);

bg = (ImageView)findViewById(R.id.overlay);
bg.setOnClickListener(this);

ActionBar acb = getActionBar();
acb.hide();
}

private List<String> addItems(String[] values) {


List<String> list = new ArrayList<String>();
for (String var : values) {
list.add(var);
}


return list;
}


private List<Integer> addItems(Integer[] values) {


List<Integer> list = new ArrayList<Integer>();
for (Integer var : values) {
list.add(var);
}


return list;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");
return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onMenuOpened(int featureId, Menu menu) {


if (myPopupMenu.isShowing()) {
myPopupMenu.dismiss();
} else {
myPopupMenu.show();
}
return false;
}

public void showpopupwindow(View view){
if (myPopupMenu.isShowing()) {
myPopupMenu.dismiss();
} else {
myPopupMenu.setAnchorView(view);
myPopupMenu.show();
bg.setVisibility(View.VISIBLE);
}
}


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id = arg0.getId();
if(id == R.id.overlay){

bg.setVisibility(View.GONE);
}
}


@Override
public void onDismiss() {
// TODO Auto-generated method stub
bg.setVisibility(View.GONE);
}


}


package com.example.mypopupwindow;


public interface OnDismiss {


public void onDismiss();
}



package com.example.mypopupwindow;


import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class BodyAdapter extends BaseAdapter{


private List<String> item_names;
private List<Integer> item_images;
private Context mContext;

public BodyAdapter(Context context,List<String> item_names,List<Integer> item_images){
this.mContext = context;
this.item_names = item_names;
this.item_images = item_images;
}

@Override
public int getCount() {
return item_images.size();
}


@Override
public Object getItem(int position) {
return position;
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

final LayoutInflater inflater = LayoutInflater.from(mContext);
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.pada_browser_menu_item, parent,
false);
}
TextView title = (TextView) view.findViewById(R.id.item_title);
title.setText(item_names.get(position));

   return view;
}

}


然后再定义两个xml文件


pada_browser_menu_item.xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   >
    
    <TextView 
       android:id="@+id/item_title"
       android:layout_width="wrap_content"
    android:layout_height="100px"
    android:textSize="22dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="18dp"
    android:gravity="center"
       />


</RelativeLayout>

你可能感兴趣的:(点击主界面 PopupMenu消失)