java - Android 中类似 facebook 的 react 按钮

标签 java android position android-dialogfragment dialogfragment

我想做一个像 Facebook 这样的 react 按钮,所以像这样
enter image description here
现在我设法用 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);
    }


}

最佳答案

使用弹出窗口。我认为这正是您正在寻找的:
https://developer.android.com/reference/android/widget/PopupWindow
您可以使用 this method 设置弹出窗口的确切位置。

关于java - Android 中类似 facebook 的 react 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69802874/

相关文章:

java - Gradle mavenDeployer - 如何包含单独的模块代码

java - 从 Java 运行的 Groovy 脚本是否具有相同的类路径?

java - 如何使用 apache log4j2 功能和 log4j2 配置文件写入 CSV 文件?

android - 替换最近应用程序菜单中的图像

java - Activity 堆栈问题

css - 由于 CSS 定位,IE 中的 Div 位置不正确对齐

linux - 我可以使用命令行或 perl 更改终端位置吗?

java - native 方法链接错误

android - 椭圆大小不适用于 Textview

html - 获取 Canvas 触摸事件的正确坐标