Android 中类似 facebook 的反应按钮

Reaction button like facebook in Android

我想做一个像 Facebook 这样的反应按钮,所以像这样

现在我设法用 DialogFragment 做了一些类似的事情,但是我在对话框的位置上遇到了一些问题,这段代码有时可以工作,但有时对话框会放在错误的位置。有人可以给我一些建议或建议我更好的方法吗?

我的对话框Class


public class ReactionsFragment extends DialogFragment {

    public static final String TAG = "ReactionsFragment";
    private  View paretnView;


    public ReactionsFragment(View view, String reviewId,Reactionable reactedContent) {
        this.paretnView = view;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_reactions, container, false);
        initComponent(view);
        setDialogPosition();
        return view;
    }


   
    public void initComponent(View rootView) {...}


    public void setDialogPosition() {
        if (paretnView == null) {
            return; // Leave the dialog in default position
        }
        int[] location = new int[2];
        paretnView.getLocationOnScreen(location);
        int sourceX = location[0];
        int sourceY = location[1];
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.TOP|Gravity.LEFT);
        WindowManager.LayoutParams params = window.getAttributes();
        params.x = sourceX - dpToPx(0);
        params.y = sourceY - dpToPx(110);
        window.setAttributes(params);
    }


    public int dpToPx(float valueInDp) {
        DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
    }


}

使用弹出窗口。我认为这正是您要寻找的: https://developer.android.com/reference/android/widget/PopupWindow

您可以使用 this method

设置弹出窗口出现的确切位置