PopuWindow的使用

PopuWindow的使用

1.在activity_main.xml文件中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rel" tools:context="www.weshared.popuwindowdemo.MainActivity">

    <TextView  android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@android:color/holo_red_light" android:gravity="center" android:padding="16dp" android:text="Hello World!" />
</RelativeLayout>

2.popuwindow中的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/darker_gray" android:orientation="vertical" tools:context="www.weshared.popuwindowdemo.MainActivity">

    <TextView  android:id="@+id/tv_1" style="@style/popu" android:layout_marginBottom="1px" android:text="后台打开" />

    <TextView  android:id="@+id/tv_2" style="@style/popu" android:layout_marginBottom="1px" android:text="新窗口打开" />

    <TextView  android:id="@+id/tv_3" style="@style/popu" android:layout_marginBottom="1px" android:text="复制链接地址" />

    <TextView  android:id="@+id/tv_4" style="@style/popu" android:text="选择复制" />
</LinearLayout>

3.style.xml文件中,自定义一个popu的样式

<!--android:layout_width="120dp"-->
<!--android:layout_height="40dp"-->
<!--android:gravity="center_vertical"-->
<!--android:background="@android:color/black"-->
<!--android:paddingLeft="8dp"-->
<!--android:textColor="@android:color/white"-->
<style name="popu"> <item name="android:layout_width">120dp</item> <item name="android:layout_height">40dp</item> <item name="android:gravity">center_vertical</item> <item name="android:background">@android:color/black</item> <item name="android:paddingLeft">8dp</item> <item name="android:textColor">@android:color/white</item> </style>

4.在MainActivity文件中

public class MainActivity extends AppCompatActivity implements View.OnLongClickListener, View.OnTouchListener, View.OnClickListener {

    private int x;
    private int y;
    private PopupWindow popu;

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

    private void init() {
        RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.rel);
        TextView mTextView = (TextView) findViewById(R.id.tv);
        if (mTextView == null || mRelativeLayout == null) {
            return;
        }
        mTextView.setOnTouchListener(this);
        mTextView.setOnClickListener(this);
        mTextView.setOnLongClickListener(this);
        mRelativeLayout.setOnClickListener(this);
    }

    private void initPopu(View v) {
        //弹出一个popuwindow
        View view = LayoutInflater.from(this).inflate(R.layout.popu_layout, null);
        popu = new PopupWindow(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popu.setContentView(view);
        //展示的位置
        popu.showAtLocation((View) v.getParent(), Gravity.NO_GRAVITY, x, y);//这里显示的效果是,按住哪里,锚点就在哪里
        //popu展示的位置,是通过控件作为锚点,x,y是通过params设置离锚点的位置。
// popu.showAsDropDown(v);
// popu.showAsDropDown(v,x,y);
// popu.showAsDropDown(v,x,y,Gravity.START|Gravity.LEFT);//默认就是Gravity.START|Gravity.LEFT

        final TextView tv1 = (TextView) view.findViewById(R.id.tv_1);
        TextView tv2 = (TextView) view.findViewById(R.id.tv_2);
        TextView tv3 = (TextView) view.findViewById(R.id.tv_3);
        TextView tv4 = (TextView) view.findViewById(R.id.tv_4);

        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popu!=null){
                    popu.dismiss();
                }
                Toast.makeText(getApplicationContext(),tv1.getText(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onLongClick(View v) {
        initPopu(v);
        return true;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //通过触摸的监听事件,拿到绝对坐标的x,y值
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            x = (int) event.getRawX();
            y = (int) event.getRawY();
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        //当popu显示是,点击消失
        if (popu != null && popu.isShowing()) {
            popu.dismiss();
        }
    }
}

你可能感兴趣的:(android,PopuWindow)