我遵循了本网站中的一些问题和示例并实施了以下操作,但对话框仍显示在左上角而不是屏幕中间。
我想让背景变暗并将对话框定位在屏幕中央...但是如何修改以下内容?提前致谢!!
@Override
public void onBackPressed()
{
final Dialog dialog1 = new Dialog(First.this, android.R.style.Theme_Translucent_NoTitleBar);
WindowManager.LayoutParams lp = dialog1.getWindow().getAttributes();
lp.dimAmount = 0.5f;
dialog1.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Window window = dialog1.getWindow();
window.setGravity(Gravity.CENTER);
dialog1.setContentView(R.layout.alert_dialog);
dialog1.setCancelable(true);
TextView thankyou = (TextView) dialog1.findViewById(R.id.thankyou);
TextView Title_V1 = (TextView) dialog1.findViewById(R.id.Title_V1);
Button alert_cancel = (Button) dialog1.findViewById(R.id.button_cancel);
Button alert_quit = (Button) dialog1.findViewById(R.id.button_ok);
thankyou.setText("Quit?");
Title_V1.setText("");
alert_cancel.setText("Cancel");
alert_quit.setText("Quit!");
dialog1.show();
alert_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="30dp"
android:background="@drawable/layout_round_corners"
android:gravity="center" >
<TextView
android:id="@+id/thankyou"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="8dp"
android:layout_marginTop="5dp"
android:paddingBottom="10dp"
android:paddingTop="15dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/dark_green"
android:textSize="25dp"
android:textStyle="bold" />
<View
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/thankyou"
android:background="@color/green"
android:paddingTop="20dp" />
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/view1" >
<TableLayout
android:id="@+id/Table1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="@+id/Title_V1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="@color/green"
android:textSize="25dp"
android:textStyle="bold" />
</TableLayout>
</ScrollView>
<LinearLayout
android:id="@+id/LinearLayout_Row_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/ScrollView01"
android:gravity="center"
android:orientation="horizontal"
android:padding="15dp" >
<Button
android:id="@+id/button_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="0.5"
android:background="@drawable/green_btn"
android:textColor="@color/white"
android:textSize="@dimen/dp25_heading"
android:textStyle="bold" />
<Button
android:id="@+id/button_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.5"
android:background="@drawable/green_btn"
android:textColor="@color/white"
android:textSize="@dimen/dp25_heading"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>