【问题标题】:Reaction button like facebook in AndroidAndroid 中类似 facebook 的反应按钮
【发布时间】:2021-12-16 12:51:05
【问题描述】:

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

现在我设法用 DialogFragment 制作类似的东西,但我的对话框位置有些问题,此代码有时有效,但有时对话框放置在错误的位置。有人可以给我一些建议或建议我更好的方法吗?

我的对话类


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


}

【问题讨论】:

    标签: java android position android-dialogfragment dialogfragment


    【解决方案1】:

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

    您可以使用this method设置弹出窗口的确切位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      相关资源
      最近更新 更多