android自定义对话框放在中心与背景变暗

时间:2013-07-27 05:03:45

标签: android dialog gravity

我已经按照本网站上的一些问题和示例进行了实施,但是对话框仍然显示在左上角而不是屏幕中间。

我想让背景变暗并在屏幕中央找到对话框...但是如何修改以下内容?在此先感谢!!

    @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>

1 个答案:

答案 0 :(得分:2)

为您的相对布局设置一个父布局,并为fill_parentandroid:layout_width

提供android:layout_height
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >
// your entire layout and no need to set gravity for your child relative layout.
</RelativeLayout>

现在无需为您的孩子RelativeLayout设置重力,即使是窗户也是如此 并且是的,使您的父视图背景透明:

android:background="@android:color/transparent"