【发布时间】: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