MainActivity.java

public class MainActivity extends Activity implements OnClickListener{ private PopMenu popMenu;private Context context;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);context = MainActivity.this;button=(Button)findViewById(R.id.bt);button.setOnClickListener(this);popMenu = new PopMenu(context);popMenu.setOnItemClickListener( new OnItemClickListener() {//每个条目的点击事件@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) { //每个条目的点击响应事件popMenu.dismiss();}}  );  }// 弹出菜单监听器@Overridepublic void onClick(View v) {popMenu.showAsDropDown(v);}}

PopMenu.java
public class PopMenu {private ArrayList<String> itemListStr ;private ArrayList<Integer> itemListInt;private Context context;private PopupWindow popupWindow;private ListView listView;public PopMenu(Context context) {this.context = context;itemListStr = new ArrayList<String>(); itemListInt=new ArrayList<Integer>();String[] str=new String[]{"你好", "我好", "大家好", "哈哈"};int[] i=new int[]{R.drawable.picture0,R.drawable.picture1,R.drawable.picture2,R.drawable.picture3};for(String s:str){itemListStr.add(s);}for(int img:i){Integer listImgInteger=Integer.valueOf(img);//int-->IntegeritemListInt.add(listImgInteger);}Log.i("Tag","itemListStr______"+itemListStr);Log.i("Tag","itemListInt________"+itemListInt);View view = LayoutInflater.from(context).inflate(R.layout.poplist, null);//PopupWindow里面的viewlistView = (ListView) view.findViewById(R.id.menu_listview);listView.setAdapter(new PopMenuAdapter(context,itemListStr,itemListInt));listView.setFocusableInTouchMode(true);listView.setFocusable(true);popupWindow = new PopupWindow(view, 300, LayoutParams.WRAP_CONTENT);popupWindow.setBackgroundDrawable(new BitmapDrawable()); // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景(很神奇的),}// 设置每个条目击监听事件,前面会调用这个方法public void setOnItemClickListener(OnItemClickListener listener) {listView.setOnItemClickListener(listener);} // 下拉式 弹出 pop菜单 parent 右下角public void showAsDropDown(View parent) {popupWindow.showAsDropDown(parent,-150,0);//showAsDropDown(View anchor, int xoff, int yoff)弹出对话框,位置在紧挨着view组件,x y 代表着偏移量 popupWindow.setFocusable(true);//必须要获得焦点哦,不然里面的控件啥的点击无效的哦popupWindow.setOutsideTouchable(true);// 设置允许在外点击消失popupWindow.update();// 刷新状态}// 隐藏菜单public void dismiss() {popupWindow.dismiss();}}
PopMenuAdapter.java

public class PopMenuAdapter extends BaseAdapter {ArrayList<String> itemListS;ArrayList<Integer> itemListI;Context mcon;public PopMenuAdapter(Context mcon,ArrayList<String> itemListStr,ArrayList<Integer> itemListImg){this.mcon=mcon;this.itemListS=itemListStr;this.itemListI=itemListImg;}@Overridepublic int getCount() {return itemListS.size();}@Overridepublic Object getItem(int position) {return itemListS.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder = null;if (convertView == null) {convertView = LayoutInflater.from(mcon).inflate(R.layout.showpoplist, null);holder = new ViewHolder();holder.imageView = (ImageView) convertView.findViewById(R.id.img);holder.groupItem = (TextView) convertView.findViewById(R.id.textview);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}holder.imageView.setBackgroundResource(itemListI.get(position).intValue());//Integer-->intholder.groupItem.setText(itemListS.get(position));Log.i("Tag","itemListS.get(position)~~~~~~"+itemListS.get(position));Log.i("Tag","itemListI.get(position).intValue()~~~~~"+itemListI.get(position).intValue());return convertView;}private final class ViewHolder {ImageView imageView;TextView groupItem;}}

两个布局文件,一个是PopMenu的填充的view,里面就是ListView;另一个布局文件是ListView每个条目的布局。

poplist.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"       android:paddingLeft="20dip"     android:gravity="right"     android:id="@+id/hotalk_menu_view_layout" >          <!-- 显示的listview -->          <ListView android:id="@+id/menu_listview"             android:layout_width="fill_parent"              android:layout_height="wrap_content"               android:focusable="true"             android:longClickable="true"             android:scrollbarSize="0sp"                     android:scrollbarStyle="insideOverlay"             android:background="@drawable/menubg"             android:divider="@drawable/group_divider"             android:dividerHeight="1px"             android:cacheColorHint="#00000000">     </ListView>    </RelativeLayout> 

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"   >    <!-- 显示内容 ,每个条目--><ImageView    android:id="@+id/img"    android:layout_width="60dp"        android:layout_height="40dp"        android:layout_alignParentLeft="true"     />    <TextView        android:id="@+id/textview"        android:layout_width="200dp"        android:layout_height="wrap_content"        android:gravity="center_vertical"       android:layout_toRightOf="@id/img"       android:layout_alignBottom="@id/img"        android:textSize="25sp"        android:textColor="#FF33CC" /></RelativeLayout>



更多相关文章

  1. Android实现点击切换视图并跳转传值
  2. Android轮播广告条NoticeView
  3. Android(安卓)ListView 中的CheckBox点击乱系
  4. 布局参数说明及长按某区域出现菜单
  5. android 可以控制速度的跑马灯
  6. android XMl Selector 图片背景点击和焦点获取样式
  7. Android(安卓)recyclerview 支持网格布局的间隙平均分割
  8. listview使用ArrayAdapter显示文字
  9. ch05 Android布局

随机推荐

  1. Android腾讯微薄客户端开发六:给用户加VI
  2. Android调用第三方app(Scheme隐式以及显
  3. INSTALL_FAILED_TEST_ONLY的原因
  4. 项目运行报错Error: Static interface me
  5. Activity启动模式(launchMode)详解
  6. NDK build编译的解析
  7. android retrofit End of input at line
  8. Android(安卓)P Camera App(一)
  9. android fih-mms的实现
  10. android如何将生成的图片保存至手机相册