我有一个自定义对话框(cameraTipDialog ctd
),我需要将其定位在某个(x,y)位置。该位置基于按钮在屏幕上的位置。我需要将对话框放在位置:
(int) ((ImageButton) findViewById(R.id.infoButton)).getY() - HEIGHT OF MY DIALOG BOX;'
但是我不知道如何获得自定义对话框的高度。它的高度是“包装内容”,所以当我执行getLayoutParams.getHeight()时,我得到-1。
在StackOverflow上遵循其他想法,我尝试使用(ctd.findViewById(R.id.dialog_root_layout)).getHeight()
,但该应用程序崩溃,因为它找不到ID为dialog_root_layout的项目。
显示布局代码:
cameraTipDialog ctd = new cameraTipDialog(submitHome.this);
Window window = ctd.getWindow();
(ctd.findViewById(R.id.dialog_root_layout)).getHeight()
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.TOP | Gravity.LEFT;
wlp.x = (int) ((ImageButton) findViewById(R.id.infoButton)).getX();
wlp.y = (int) ((ImageButton) findViewById(R.id.infoButton)).getY() - DIALOG HEIGHT;
window.setAttributes(wlp);
ctd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
ctd.show();
对话框XML代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dialog_root_layout"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginLeft="16sp"
android:layout_marginRight="16dp"
android:background="@drawable/popup_background">
<TextView
android:id="@+id/content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="32dp"
android:text="@string/tipsForSuccess"
android:textColor="@android:color/white"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/header" />
<TextView
android:id="@+id/header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="text here."
android:textColor="@android:color/white"
android:textSize="10sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
答案 0 :(得分:0)
要获取实际高度,可以在getRootView().getMeasuredHeight()
上使用dialog_root_layout
。但是,只有在测量视图后才能执行此操作,因此您需要在OnShowListener
中进行计算。修改后的代码如下:
cameraTipDialog ctd = new cameraTipDialog(this);
ctd.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Dialog ctd = (Dialog) dialog;
View dialogRootLayout = ctd.findViewById(R.id.dialog_root_layout);
int dialogHeight = dialogRootLayout.getRootView().getMeasuredHeight();
Window window = ctd.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.TOP | Gravity.LEFT;
wlp.x = (int) ((ImageButton) findViewById(R.id.infoButton)).getX();
wlp.y = (int) ((ImageButton) findViewById(R.id.infoButton)).getY() - dialogHeight;
window.setAttributes(wlp);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
});
ctd.show();