`
阅读更多
private void iniPopUpWindow()
    {
        final String[] s=new String[]{"设置","均衡器","意见反馈 ","皮肤选择","本地扫描","流量控制","定时音乐","退出"};
        View popview = getLayoutInflater().inflate(R.layout.popupwindow, null);
        View emptyview = (View) popview.findViewById(R.id.view1);
        GridView gridView = (GridView) popview.findViewById(R.id.gridView1);
        //如果设置的 是空白的emptyview.setOnClickListener()不起作用,.......
        //设置的是popview.setOnClickListener()则起作用
        popview.setOnClickListener(new OnClickListener()
        {
           
            @Override
            public void onClick(View v)
            {
               popupWindow.dismiss();               
            }
        });
        gridView.setAdapter(new BaseAdapter()
        {
           
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                convertView= getLayoutInflater().inflate(R.layout.item_popup, null);
                TextView text = (TextView) convertView.findViewById(R.id.textView1);
                ImageView image = (ImageView) convertView.findViewById(R.id.imageView1);
                text.setText(s[position]);
                return convertView;
            }
           
            @Override
            public long getItemId(int position)
            {
                return 0;
            }
           
            @Override
            public Object getItem(int position)
            {
                return null;
            }
           
            @Override
            public int getCount()
            {
                // TODO Auto-generated method stub
                return s.length;
            }
        });
        gridView.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
              Toast.makeText(HomePage.this, position+"", Toast.LENGTH_SHORT).show();
              popupWindow.dismiss();
            }
        });
        //下面设置了popupwindow一显示就获得焦点,activity就 没有 焦点,监听不到事件,只要gridview有显示,不管按什么键  popupwindow都得消失
       
        gridView.setOnKeyListener(new OnKeyListener()
        {
           
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if(keyCode==KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN)
                {
                    popupWindow.dismiss();
                }
                if(keyCode==KeyEvent.KEYCODE_MENU  && event.getAction()==KeyEvent.ACTION_DOWN)
                {
                    popupWindow.dismiss();
                }
                if(keyCode==KeyEvent.KEYCODE_HOME  && event.getAction()==KeyEvent.ACTION_DOWN)
                {
                    popupWindow.dismiss();
                }
                return false;
            }
        });
        //popupWindow的高度要wrap content 不然  显示的gridview  在头部,不会在底部
        popupWindow = new PopupWindow(popview, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
//        popupWindow.setOutsideTouchable(true);
//        //touch  拦截器
//        popupWindow.setTouchInterceptor(new OnTouchListener()
//        {
//           
//            @Override
//            public boolean onTouch(View v, MotionEvent event)
//            {
//                //不管3 7  21 按了外面popupWindow  就消失
//                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
//
//                    popupWindow.dismiss();
//                }
//              
//                return false;
//            }
//        });
      
    }


//重写activity 的
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        //只要popupwindow没有显示,焦点就在activity上,可以监听到按了menu键....
        if(keyCode==KeyEvent.KEYCODE_MENU)
        {
            popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
//重写activity的  默认是返回true  这里改为false不要系统默认的菜单
   @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        //不要系统的option menu
        return false;
    }


popupwindow.xml   弹出窗口的布局
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

       

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
        <GridView
            android:id="@+id/gridView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="4" >
        </GridView>

    </LinearLayout>

item_popup.xml    gridview的item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
//这里设置match parent没问题,因为它针对的是gridview 设置的    numColumns="4" 的column的宽而不是针对gridview的宽
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="62dp"
        android:layout_height="62dp"
        android:src="@drawable/ic_find" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics