工作_点击弹出窗口

package com.demo.pop.mypop;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mBt1;
    private Button mBt2;
    private MyPopView mMpv;
    private View mInflate;
    private View mMInflate;
    private Button mBt_c;

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

        initView();
    }

    private void initView() {
        mBt1 = findViewById(R.id.bt1);
        mBt2 = findViewById(R.id.bt2);
        mMpv = findViewById(R.id.mpv);
        mBt_c =findViewById(R.id.bt_c);
        mBt1.setOnClickListener(this);
        mBt2.setOnClickListener(this);
        mBt_c.setOnClickListener(this);

        mMInflate = View.inflate(this, R.layout.layout_content, null);
        Button tv_1 = mMInflate.findViewById(R.id.tv_1);
        Button tv_2 = mMInflate.findViewById(R.id.tv_2);
        Button tv_3 = mMInflate.findViewById(R.id.tv_3);
        tv_1.setOnClickListener(this);
        tv_2.setOnClickListener(this);
        tv_3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int[] location = new int[2];
        switch (v.getId()) {
            case R.id.bt1:
                mBt1.getLocationOnScreen(location);
                mMpv.setView(mMInflate, mBt1, location[1]);
                break;
            case R.id.bt2:
                mBt2.getLocationOnScreen(location);
                mMpv.setView(mMInflate, mBt2, location[1]);
                break;
            case R.id.bt_c:
                mBt_c.getLocationOnScreen(location);
                mMpv.setView(mMInflate, mBt_c, location[1]);
                break;
            case R.id.tv_1:
            case R.id.tv_2:
            case R.id.tv_3:
                Log.e("1123", "dismiss");
                mMpv.dismiss();
                break;
        }
    }
}
package com.demo.pop.mypop;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

public class MyPopView extends FrameLayout {
    public MyPopView(Context context) {
        this(context, null);
    }

    public MyPopView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyPopView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setView(View content, View click, int clickLocation) {
        removeAllViews();

        int[] location = new int[2];
        getLocationOnScreen(location);

        int startY = -location[1] + click.getMeasuredHeight() + clickLocation;
        content.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int height = content.getMeasuredHeight();
        LayoutParams lp = new LayoutParams(getScreenWidth(), getScreenHeight());
        lp.leftMargin = 0;
        lp.topMargin = startY;
        addView(content, lp);

    }

    public void dismiss() {
        removeAllViews();
    }

    public int getScreenWidth() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度(像素)
        return width;
    }

    public int getScreenHeight() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int height = dm.heightPixels;       // 屏幕高度(像素)
        return height;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/bt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="bt_1" />

            <Button
                android:id="@+id/bt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="bt_2" />
        </LinearLayout>

        <Button
            android:id="@+id/bt_c"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="C" />
    </RelativeLayout>


    <com.demo.pop.mypop.MyPopView
        android:id="@+id/mpv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.demo.pop.mypop.MyPopView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:orientation="vertical">

    <Button
        android:id="@+id/tv_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="tv_1" />

    <Button
        android:id="@+id/tv_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="tv_2" />

    <Button
        android:id="@+id/tv_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="tv_3" />
</LinearLayout>
发布了75 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/sinat_40387150/article/details/97040195