简体   繁体   English

Android 中类似 Facebook 的反应按钮

[英]Reaction button like facebook in Android

I wanna make a reaction button like Facebook, so something like this我想做一个像 Facebook 这样的反应按钮,所以像这样

在此处输入图片说明

Now i manage to make something similar with a DialogFragment but I am having some trouble with the position of my dialog, this code sometimes works but some other time the dialog is placed in the wrong place.现在我设法用 DialogFragment 制作类似的东西,但我在对话框的位置上遇到了一些问题,这段代码有时有效,但有时对话框放置在错误的位置。 Can someone give me some advice or suggest me a better way to do this?有人可以给我一些建议或建议我这样做的更好方法吗?

My Dialog 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);
    }


}

Use PopupWindow.使用弹出窗口。 I think this is exactly what you are looking for: https://developer.android.com/reference/android/widget/PopupWindow我认为这正是您要寻找的: https : //developer.android.com/reference/android/widget/PopupWindow

You can set the exact position for the popup to appear using this method您可以使用此方法设置弹出窗口的确切位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM