如何在垂直居中显示自定义对话框
How to show custom dialog at Center Vertical
查看屏幕截图,对话框未显示垂直居中和意外边距
我试过很多例子,但没有人解决这个问题。
请参考屏幕截图,这样您就会很容易理解我的问题是什么。
"As you see - Dialog appear with unexpected margin"
我也试过这个
Window window = dialog.getWindow();
window.setLayout(AppBarLayout.LayoutParams.WRAP_CONTENT,
AppBarLayout.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
java 代码是 -
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialogforgot_password);
final EditText et_emailforgot = (EditText)dialog.findViewById(R.id.et_EnterMail);
final Button bt_SubmitForgotPass = (Button)dialog.findViewById(R.id.bt_SubmitForgotButton);
final TextView ForgotHeading = (TextView)dialog.findViewById(R.id.tv_forgotpasswordHeading);
final TextView PleaseEnterurMail = (TextView)dialog.findViewById(R.id.tv_Enteruremail);
et_emailforgot.setTypeface(Lato_Regular);
PleaseEnterurMail.setTypeface(Lato_Regular);
ForgotHeading.setTypeface(Lato_Bold);
bt_SubmitForgotPass.setTypeface(Lato_Bold);
dialog.show();
我的 XML 文件如下:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_windowcorner"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/rounded_headingcorner"
android:layout_alignParentTop="true"
android:id="@+id/RLForgot">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forgot Password"
android:textColor="#ffffff"
android:textSize="16dp"
android:id="@+id/tv_forgotpasswordHeading"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please Enter Your Registered Email id"
android:textColor="#000000"
android:textSize="17dp"
android:layout_marginRight="22dp"
android:layout_marginLeft="22dp"
android:id="@+id/tv_Enteruremail"
android:layout_centerHorizontal="true"
android:layout_below="@+id/RLForgot"
android:layout_marginTop="20dp"
android:textAlignment="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:textAlignment="center"
android:id="@+id/et_EnterMail"
android:hint="Enter Your Email Id Here"
android:singleLine="true"
android:background="@drawable/custom_spinner_background"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:layout_below="@+id/tv_Enteruremail"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Submit"
android:background="@drawable/signin_btn"
android:textColor="@color/signinlogcolor"
style="?android:attr/borderlessButtonStyle"
android:id="@+id/bt_SubmitForgotButton"
android:layout_marginTop="30dp"
android:layout_marginBottom="10dp"
android:layout_below="@+id/et_EnterMail"
android:layout_centerHorizontal="true" />
</RelativeLayout>
what my problem is. "As you see - Dialog appear with unexpected margin"
它被称为 Dialog
title 使用 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
从 Dialog
中删除标题,例如以下 code
试试这个
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialogforgot_password);
There isn't any unexpected margin in your dialog
您的对话框已经垂直居中对齐。因为你有标题栏,所以好像不对。
放
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
对话框初始化后
查看屏幕截图,对话框未显示垂直居中和意外边距
我试过很多例子,但没有人解决这个问题。 请参考屏幕截图,这样您就会很容易理解我的问题是什么。 "As you see - Dialog appear with unexpected margin"
我也试过这个
Window window = dialog.getWindow(); window.setLayout(AppBarLayout.LayoutParams.WRAP_CONTENT, AppBarLayout.LayoutParams.WRAP_CONTENT); window.setGravity(Gravity.CENTER);
java 代码是 -
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialogforgot_password);
final EditText et_emailforgot = (EditText)dialog.findViewById(R.id.et_EnterMail);
final Button bt_SubmitForgotPass = (Button)dialog.findViewById(R.id.bt_SubmitForgotButton);
final TextView ForgotHeading = (TextView)dialog.findViewById(R.id.tv_forgotpasswordHeading);
final TextView PleaseEnterurMail = (TextView)dialog.findViewById(R.id.tv_Enteruremail);
et_emailforgot.setTypeface(Lato_Regular);
PleaseEnterurMail.setTypeface(Lato_Regular);
ForgotHeading.setTypeface(Lato_Bold);
bt_SubmitForgotPass.setTypeface(Lato_Bold);
dialog.show();
我的 XML 文件如下:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_windowcorner"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/rounded_headingcorner"
android:layout_alignParentTop="true"
android:id="@+id/RLForgot">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forgot Password"
android:textColor="#ffffff"
android:textSize="16dp"
android:id="@+id/tv_forgotpasswordHeading"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please Enter Your Registered Email id"
android:textColor="#000000"
android:textSize="17dp"
android:layout_marginRight="22dp"
android:layout_marginLeft="22dp"
android:id="@+id/tv_Enteruremail"
android:layout_centerHorizontal="true"
android:layout_below="@+id/RLForgot"
android:layout_marginTop="20dp"
android:textAlignment="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:textAlignment="center"
android:id="@+id/et_EnterMail"
android:hint="Enter Your Email Id Here"
android:singleLine="true"
android:background="@drawable/custom_spinner_background"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:layout_below="@+id/tv_Enteruremail"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Submit"
android:background="@drawable/signin_btn"
android:textColor="@color/signinlogcolor"
style="?android:attr/borderlessButtonStyle"
android:id="@+id/bt_SubmitForgotButton"
android:layout_marginTop="30dp"
android:layout_marginBottom="10dp"
android:layout_below="@+id/et_EnterMail"
android:layout_centerHorizontal="true" />
</RelativeLayout>
what my problem is. "As you see - Dialog appear with unexpected margin"
它被称为 Dialog
title 使用 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
从 Dialog
中删除标题,例如以下 code
试试这个
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialogforgot_password);
There isn't any unexpected margin in your dialog
您的对话框已经垂直居中对齐。因为你有标题栏,所以好像不对。
放
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
对话框初始化后