Android 无法将对话框定位在视图的中心
我想在特定布局的中心点放置一个自定义对话框。 但是,对话框不会转到中心。其实我试过各种方法,但现在我被卡住了。。。真头痛。 我试着像下面的代码Android 无法将对话框定位在视图的中心,android,dialog,position,center,Android,Dialog,Position,Center,我想在特定布局的中心点放置一个自定义对话框。 但是,对话框不会转到中心。其实我试过各种方法,但现在我被卡住了。。。真头痛。 我试着像下面的代码 showCustomDialog(){ View view = (View) getLayoutInflater().inflate(R.layout.abc, null); view.measure(view.MeasureSpec.UNSPECIFIED, view.MeasureSpec.UNSPEC
showCustomDialog(){
View view = (View) getLayoutInflater().inflate(R.layout.abc, null);
view.measure(view.MeasureSpec.UNSPECIFIED, view.MeasureSpec.UNSPECIFIED);
LinearLayout BlackBox = (LinearLayout) view.findViewById(R.id.txt_layout);
Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.x=blackBox.getMeasuredWidth()/2;
params.y=blackBox.getMeasuredHeight()/2;
System.out.println("params " + params.x + " " + params.y);
dialog.getWindow().setAttributes(params);
dialog.getWindow().setLayout(view.getMeasuredWidth(), WindowManager.LayoutParams.WRAP_CONTENT);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.cust_dialog);
dialog.show();
}
问题是我想在名为layout的“txt_布局”的中心显示对话框。
感谢您阅读此文章。如果您以前没有尝试过,请尝试以下内容:
Window window = customdialog.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
在xml中,请检查是否设置了以下参数:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PauseDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowTitleStyle">@style/PauseDialogTitle</item>
</style>
<style name="PauseDialogTitle" parent="@android:style/TextAppearance.DialogWindowTitle">
<item name="android:gravity">center_horizontal</item>
</style>
<style name="DialogWindowTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">@android:style/TextAppearance.DialogWindowTitle</item>
</style>
</resources>
@样式/暂停对话框
水平中心
1.
真的
@android:style/TextAppearance.DialogWindowTitle
您的布局应该如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
>
实际上,我检查了params.y是否正常工作,因为对话框看起来好像在中心(大小写高度),但对于宽度,对话框没有定位在宽度的中心。对不起,我的英语添加了dialog.setGravity(Gravity.CENTER)
或居中垂直|居中水平
@skizo我试过你的方法。对话框显示在整个视图的中心。我们想要的是使对话框显示在txt_布局视图的中心…这只是一个如何制作自定义对话框的教程,请阅读我的问题