doum
6 天以前 2b287056e2f59518888d05a1bbc7e5a55fbd84d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.example.datalibrary.utils;
 
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
 
import androidx.annotation.RequiresApi;
 
import com.example.datalibrary.R;;
 
 
public class PWTextUtils {
    private static PopupWindow popupWindow;
    private static PopupWindow.OnDismissListener onDismissListener;
 
    public static void setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) {
        PWTextUtils.onDismissListener = onDismissListener;
    }
 
    /**
     * @param target    显示在哪个View下方
     * @param reference 使用此View在屏幕上的X坐标来控制显示View的X坐标
     * @param context
     * @param text      需要显示的内容
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static void showDescribeText(View target, View reference, Context context,
                                        String text, int showWidth, int showXLocation) {
        popupWindow = new PopupWindow();
        if (onDismissListener != null) {
            popupWindow.setOnDismissListener(onDismissListener);
        }
        popupWindow.setWidth(showWidth);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        View pView = LayoutInflater.from(context).inflate(R.layout.popupwindow_text, null);
        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.cw_round));
        TextView textView = pView.findViewById(R.id.showText);
        textView.setText(text);
        popupWindow.setContentView(pView);
        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.sr_pw_rectangle));
        popupWindow.showAsDropDown(target, showXLocation, 0, Gravity.CENTER);
    }
 
    public static int getTargetX(View view) {
        int[] location = new int[2];
        view.getLocationInWindow(location); // 获取在当前窗口内的绝对坐标
        return location[0];
    }
 
    public static void closePop(Window window) {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.alpha = 1.0f;
            window.setAttributes(lp);
        }
    }
 
 
}