如何在android中对齐自定义对话框中心?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10447410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to align custom dialog centre in android ?
提问by pushpa
I am working on the application where i wanted to display the dialog to be screen size. So i used code below.I got the solution through here Alert message is not displaying in alert dialog box?
我正在处理我想将对话框显示为屏幕大小的应用程序。所以我使用了下面的代码。我通过这里得到了解决方案警报消息未显示在警报对话框中?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
TextView title = new TextView(this);
title.setText("DM2");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
TextView text = new TextView(this);
text.setText("Hello This text");
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
text.setTextSize(20);
text.setGravity(Gravity.CENTER);
//Creates a linearlayout layout and sets it with initial params
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setGravity(Gravity.CENTER);
ll.addView(text);
builder.setCustomTitle(title);
builder.setPositiveButton(
"Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
Dialog d = builder.setView(ll).create();
//Fills up the entire Screen
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.FILL_PARENT;
d.show();
d.getWindow().setAttributes(lp);
But I want dialog to be appeared in center aligned. Now it is display in top of the window. I tried with lp.garvity = Gravity.center, but didn't work. and If the device orientation changes, i need to press ok button more time to close the dialog. How to fix this?
但我希望对话框以居中对齐的方式出现。现在它显示在窗口顶部。我试过 lp.garvity = Gravity.center,但没有用。并且如果设备方向发生变化,我需要按确定按钮更多时间来关闭对话框。如何解决这个问题?
Thanks in advance Pushpa
提前致谢
回答by Woody
I'm little late, but better late then never :-) you can use the code below: (I saw also here)
我有点晚了,但迟到总比没有好:-)你可以使用下面的代码:(我也在这里看到)
dialog = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.activity_upload_photo_dialog);
Window window = dialog.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
//The below code is EXTRA - to dim the parent view by 70%
LayoutParams lp = window.getAttributes();
lp.dimAmount = 0.7f;
lp.flags = LayoutParams.FLAG_DIM_BEHIND;
dialog.getWindow().setAttributes(lp);
//Show the dialog
dialog.show();
Hope I helped...
希望我有所帮助...
回答by Niraj Gupta
This works for me:
这对我有用:
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
回答by Sebastian Breit
Maybe this works:
也许这有效:
WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
WMLP.gravity = Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL
dialog.getWindow().setAttributes(WMLP);
回答by DeltaCap019
Note:- This is for "custom layout"... :)
注意:- 这是用于“自定义布局”... :)
I was struggling with the same, and after struggling for hours i came to this result
我也在为此苦苦挣扎,在挣扎了几个小时后,我得出了这个结果
Earlier i was creating the dilaoge like this : -
早些时候,我正在创建这样的 dilaoge:-
final Dialog dialog = new Dialog(PopupforeditActivity.this);
After adding style "No titleBar" it started fetching the layout the way I had created.
添加样式“No titleBar”后,它开始以我创建的方式获取布局。
For this you have to create the layout with params "fillParent" and create the dialoge like this
为此,您必须使用参数“fillParent”创建布局并创建这样的对话框
final Dialog dialog = new Dialog(PopupforeditActivity.this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.dialog_billing_cart);
dialog.setCancelable(true);
The above is coding part
以上是编码部分
You in Layout
part
put your whole layout in RelativeLayout
您Layout
部分地将整个布局放入RelativeLayout
and give your layout property android:layout_centerInParent="true"
并提供您的布局属性 android:layout_centerInParent="true"
Ex:-
前任:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent" >
<!--Your Layout-->
<ScrollView
android:id="@+id/scrlView_FormHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
</ScrollView>
</RelativeLayout>
And if you don't want to miss "title" You can have that in your custom layout as TextView ..
如果您不想错过“标题”,您可以在自定义布局中将其作为 TextView ..
Hope it will help.. As it helped me :)
希望它会有所帮助..因为它帮助了我:)
回答by vishesh chandra
Try to use android:layout_gravity="center" instead of android:gravity="center"... It may be helpful for you..
尝试使用 android:layout_gravity="center" 而不是 android:gravity="center" ......它可能对你有帮助..
android:layout_gravity="center"
or
或者
you can try this..
你可以试试这个..
<activity android:theme="@android:style/Theme.Dialog"
android:name=".Splash">
</activity>
Write full xml code and make simple Activty and add one attribute in AndroidManifest.xml, when you registering your Activity into AndroidManifest.xml
编写完整的 xml 代码并制作简单的 Activty 并在 AndroidManifest.xml 中添加一个属性,当您将 Activity 注册到 AndroidManifest.xml 中时