PopupWindow 点击按钮的泡泡

PopupWindow 属性用来设置视图的位置

主java写

package com.example.popupwindow;

import com.example.popupwindow.R.drawable;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity {
public PopupWindow pw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        
    }
    public void dian(View v){//点击事件
        View view = View.inflate(this, R.layout.popup_window, null);
        GridView viewById = (GridView) view.findViewById(R.id.gv);
        viewById.setAdapter(new MyActivity());//设置九宫格
        //设置冒泡
        pw=new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT,//填充宽度
                ViewGroup.LayoutParams.WRAP_CONTENT);//包裹内容填充(内容是View的match填充父窗体,填充父窗体就是包裹内容,包裹内容-->View的填充父窗体--->(两个结合父类的wrap_..)--->GridView的包裹内容)
        pw.setBackgroundDrawable(new BitmapDrawable());//可绘制的位图
        pw.setFocusable(true);//设置可聚焦的 //显示/消失
//      pw.showAsDropDown(v);//指定在控件上的按钮的下方出现
        //显示在屏幕下方   (v是按钮v.getparent()是父布局,显示在什么位置,x水平的偏移量,y是垂直的偏移量)
        pw.showAtLocation((View) v.getParent(), Gravity.BOTTOM, 0, 0);
    
    }
    class MyActivity extends BaseAdapter{
      int[] images={
            R.drawable.i1,R.drawable.i2,
            R.drawable.i3,R.drawable.i4,
            R.drawable.i5,R.drawable.i6,
            R.drawable.i7,R.drawable.i8,
      };
      String []names={
        "1","2","3","4","5","6","7","8",      
      };
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return names[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            shuju shju=null;
            if(convertView==null){
                convertView=View.inflate(MainActivity.this, R.layout.item, null);
                shju=new shuju();
                shju.iv_icon=(ImageView) convertView.findViewById(R.id.iv_icon);
                shju.text=(TextView) convertView.findViewById(R.id.text);
                convertView.setTag(shju);
            }else{
                shju=(shuju) convertView.getTag();
            }
            shju.iv_icon.setImageResource(images[position]);
            shju.text.setText(names[position]);
            
            return convertView;
        }
        
        class shuju{
            ImageView iv_icon;
            TextView text;
        }
    }
}

主xml写



    

弹出的布局


//设置了渐变背景
    


加载布局控件GridView网格布局



    
    


效果图

PopupWindow 点击按钮的泡泡_第1张图片
image.png

自定义弹出的位置
https://www.cnblogs.com/popfisher/p/5608436.html
下载源码
https://github.com/PopFisher/SmartPopupWindow
显示在右下

int[] location = new int[2];
                view.getLocationOnScreen(location);
                mPw.showAtLocation(view, Gravity.NO_GRAVITY, location[0]+view.getWidth(), location[1]+view.getHeight());

动画
http://www.360doc.com/content/14/0418/16/11800748_370081218.shtml
显示在右下也可以用包含自带的点击效果(动画)

 // 通过WindowManager获取
        mDm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(mDm);
//                System.out.println("width-display :" + dm.widthPixels);
//                System.out.println("heigth-display :" + dm.heightPixels);
        //设置冒泡
        mPw = new PopupWindow(view,
                ViewGroup.LayoutParams.WRAP_CONTENT,//填充宽度
                ViewGroup.LayoutParams.WRAP_CONTENT);//包裹内容填充(内容是View的match填充父窗体,填充父窗体就是包裹内容,包裹内容-->View的填充父窗体--->(两个结合父类的wrap_..)--->GridView的包裹内容)
        mPw.setBackgroundDrawable(new BitmapDrawable());//可绘制的位图
        mPw.setFocusable(true);//设置可聚焦的 //显示/消失
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView adapterView, View view, int i, long l) {
                mContent = list.get(i-1).get("content");//一共有几条
                delete=i-1;//从集合中删除

                mPw.showAsDropDown(view, mDm.widthPixels,0);//他不会把自己挤没

你可能感兴趣的:(PopupWindow 点击按钮的泡泡)