DialogFragment在Android中的位置
39053 观看
8回复
300 作者的声誉
我有一个DialogFragment
表现出View
像一个弹出屏幕。窗口始终显示在屏幕中间。有没有办法设置DialogFragment
窗口的位置?我查看了源代码,但还没找到任何东西。
回应 8
90像
3021 作者的声誉
尝试这样的事情:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
getDialog().getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
p.width = ViewGroup.LayoutParams.MATCH_PARENT;
p.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
p.x = 200;
...
getDialog().getWindow().setAttributes(p);
...
或其他方法getDialog().getWindow()
。
请务必在调用set-content后设置位置。
作者: Steelight 发布者: 2012 年 4 月 24 日3像
12245 作者的声誉
4像
1339 作者的声誉
getDialog().getWindow()
如果托管活动不可见,则无法使用DialogFragment
as getWindow()
将返回null,如果您正在编写基于片段的应用程序,则不会。NullPointerException
你尝试的时候会得到一个getAttributes()
。
我推荐Mobistry的答案。如果您已经有一个DialogFragment类,那么切换并不困难。只需将onCreateDialog方法替换为构造并返回PopupWindow的方法。然后,您应该能够重复使用您提供的View AlertDialog.builder.setView()
,并进行调用(PopupWindow object).showAtLocation()
。
79像
54002 作者的声誉
是的,我用头撞了一两个小时,然后最终DialogFragment
像我想要的那样定位。
我正在建立在Steelight的答案上。这是我发现的最简单,最可靠的方法。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b) {
Window window = getDialog().getWindow();
// set "origin" to top left corner, so to speak
window.setGravity(Gravity.TOP|Gravity.LEFT);
// after that, setting values for x and y works "naturally"
WindowManager.LayoutParams params = window.getAttributes();
params.x = 300;
params.y = 100;
window.setAttributes(params);
Log.d(TAG, String.format("Positioning DialogFragment to: x %d; y %d", params.x, params.y));
}
请注意,params.width
和 params.softInputMode
(在Steelight的答案中使用)与此无关。
下面是一个更完整的例子。我真正需要的是在“源”或“父”视图旁边对齐“确认框”DialogFragment,在我的例子中是一个ImageButton。
我选择使用DialogFragment,而不是任何自定义片段,因为它为您提供免费的“对话框”功能(当用户在其外部点击时关闭对话框等)。
示例在其“源”ImageButton(垃圾桶)上方的ConfirmBox
/**
* A custom DialogFragment that is positioned above given "source" component.
*
* @author Jonik, https://stackoverflow.com/a/20419231/56285
*/
public class ConfirmBox extends DialogFragment {
private View source;
public ConfirmBox() {
}
public ConfirmBox(View source) {
this.source = source;
}
public static ConfirmBox newInstance(View source) {
return new ConfirmBox(source);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, R.style.Dialog);
}
@Override
public void onStart() {
super.onStart();
// Less dimmed background; see https://stackoverflow.com/q/13822842/56285
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.dimAmount = 0.2f; // dim only a little bit
window.setAttributes(params);
// Transparent background; see https://stackoverflow.com/q/15007272/56285
// (Needed to make dialog's alpha shadow look good)
window.setBackgroundDrawableResource(android.R.color.transparent);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Put your dialog layout in R.layout.view_confirm_box
View view = inflater.inflate(R.layout.view_confirm_box, container, false);
// Initialise what you need; set e.g. button texts and listeners, etc.
// ...
setDialogPosition();
return view;
}
/**
* Try to position this dialog next to "source" view
*/
private void setDialogPosition() {
if (source == null) {
return; // Leave the dialog in default position
}
// Find out location of source component on screen
// see https://stackoverflow.com/a/6798093/56285
int[] location = new int[2];
source.getLocationOnScreen(location);
int sourceX = location[0];
int sourceY = location[1];
Window window = getDialog().getWindow();
// set "origin" to top left corner
window.setGravity(Gravity.TOP|Gravity.LEFT);
WindowManager.LayoutParams params = window.getAttributes();
// Just an example; edit to suit your needs.
params.x = sourceX - dpToPx(110); // about half of confirm button size left of source view
params.y = sourceY - dpToPx(80); // above source view
window.setAttributes(params);
}
public int dpToPx(float valueInDp) {
DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
}
}
通过根据需要添加构造函数参数或setter,可以很容易地使上述更通用。(我的决赛ConfirmBox
有一个样式按钮(在一些边框内等),其文本View.OnClickListener
可以在代码中自定义。)
4像
323 作者的声誉
您需要覆盖DialogFragment中的onResume()方法,如下所示:
@Override
public void onResume() {
final Window dialogWindow = getDialog().getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.x = 100; // set your X position here
lp.y = 200; // set your Y position here
dialogWindow.setAttributes(lp);
super.onResume();
}
作者: Zahidur Rahman Faisal
发布者: 2016 年 7 月 16 日
1像
49 作者的声誉
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(mActivity, R.style.BottomDialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //
dialog.setContentView(R.layout.layout_video_live);
dialog.setCanceledOnTouchOutside(true);
Window window = dialog.getWindow();
assert window != null;
WindowManager.LayoutParams lp = window.getAttributes();
lp.gravity = Gravity.BOTTOM; //psotion
lp.width = WindowManager.LayoutParams.MATCH_PARENT; // fuill screen
lp.height = 280;
window.setAttributes(lp);
return dialog;
}
作者: Ven Ren
发布者: 2018 年 10 月 26 日
1像
904 作者的声誉
我使用AppCompatDialogFragment
的android.support.v7.app.AppCompatDialogFragment
,我想对话片段对齐到屏幕下方,并删除所有边界,特别是我需要设置内容宽度匹配父。
所以,我想从这里(黄色背景来自对话框片段的rootLayout):
得到这个:
上述解决方案都没有奏效。所以,我成功地做到了这一点:
fun AppCompatDialogFragment.alignToBottom() {
dialog.window.apply {
setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL)
decorView.apply {
// Get screen width
val displayMetrics = DisplayMetrics().apply {
windowManager.defaultDisplay.getMetrics(this)
}
setBackgroundColor(Color.WHITE) // I don't know why it is required, without it background of rootView is ignored (is transparent even if set in xml/runtime)
minimumWidth = displayMetrics.widthPixels
setPadding(0, 0, 0, 0)
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
invalidate()
}
}
}
作者: Cililing
发布者: 2019 年 1 月 24 日
0像
62 作者的声誉
当我遇到这个问题时,这就是我解决的问题。试试这个。
在您的DialogFragment的onActivityCreated中,根据您的需要添加此代码....
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
myview=inflater.inflate(R.layout.fragment_insurance_detail, container, false);
intitviews();
return myview;
}
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
getDialog().getWindow().setGravity(Gravity.CENTER);
}
作者: Sharon Joshi
发布者: 2019 年 7 月 22 日
来自类别的问题 :
- android Android Phone Emulator如何反映性能?
- android 如何使用保存实例状态保存Android Activity状态?
- android Android:从ListView访问子视图
- android 如何在Android上调用SOAP Web服务
- android 如何在Android中格式化日期和时间?
- android-layout 关闭/隐藏Android软键盘
- android-layout 标准Android按钮具有不同的颜色
- android-layout What is the difference between "px", "dip", "dp" and "sp"?
- android-layout 如何安排文字在图像周围流动
- android-layout 我可以在Android布局中为文本加下划线吗?
- android-fragments 如何使用以编程方式创建的内容视图向活动添加片段
- android-fragments 如何在Fragments中实现onBackPressed()?
- android-fragments 用活动组内的另一个片段替换片段
- android-fragments 如何使用片段中的XML onClick处理按钮单击
- android-fragments 有没有办法获取活动中所有当前活动片段的引用?
- android-dialogfragment 单击按钮时如何防止对话框关闭
- android-dialogfragment Full Screen DialogFragment in Android
- android-dialogfragment DialogFragment在Android中的位置
- android-dialogfragment 从onActivityResult显示DialogFragment
- android-dialogfragment Android DialogFragment onViewCreated未调用