简体   繁体   中英

How to show custom layout alert dialog at center vertical position

xml文件布局 对话框出现在父顶部,但我想在垂直中心显示它

I want to display custom dialog at center vertical position but it appears at top.Above the first one is my xml file and second one is output after running in real device. Here is the code of dialog.

    //alert dialog code
    //add theme to display on whole page
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this,      
    android.R.style.Theme_Holo_Light_NoActionBar_TranslucentDecor);
    LayoutInflater inflater = this.getLayoutInflater();
    //custom layout
    View dialogView = inflater.inflate(R.layout.custom_bar, null);
    dialogBuilder.setView(dialogView);

    //button to close dialog box
    Button closeButton = (Button)     
    dialogView.findViewById(R.id.closeButton);
    final AlertDialog alertDialog = dialogBuilder.create();
    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            alertDialog.dismiss();
        }
    });
    //show dialog
    alertDialog.show();

You should write like this.

AlertDialog dialog = new AlertDialog.Builder(this, R.style.Dark).create();
                        if (dialog.getWindow() != null)
                        {
                            Window window = dialog.getWindow();
                            window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
                            window.setGravity(Gravity.CENTER);
                            dialog.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
                        }

This will show the alert dialog to the center of the screen.

Use this code to your Alert dialog to the center of the screen.

Add this in your class and change the code as per your needed.

   //Internet Alert Dialog 
    public static void InternetAlert(final Context _A) {
        final Dialog dialog = new Dialog(_A, R.style.CustomDialogStyle);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(R.layout.dialog_internet_connection);
        final Button Yes = (Button) dialog.findViewById(R.id.btn_yes);
        Yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
    }

If you want to fill the entire screen Just change the FrameLayout Height to match_parent

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <android.support.v7.widget.CardView
        android:id="@+id/cv_SignOut"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="?attr/actionBarSize"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="?attr/actionBarSize"
        app:cardBackgroundColor="#0D000000"
        app:cardCornerRadius="6dp"
        app:cardElevation="3dp"
        app:cardUseCompatPadding="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent">

            <TextView
                android:id="@+id/title_SignOut"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@color/textViewColorPrimary"
                android:gravity="center"
                android:minHeight="?attr/actionBarSize"
                android:text="@string/info"
                android:textAppearance="?attr/textAppearanceSearchResultTitle"
                android:textColor="@color/colorWhite" />


            <TextView
                android:id="@+id/signOutAlert"
                style="@android:style/TextAppearance.DeviceDefault.SearchResult.Subtitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/title_SignOut"
                android:background="@color/colorBackground"
                android:gravity="center"
                android:minHeight="120dip"
                android:paddingBottom="@dimen/activity_horizontal_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_horizontal_margin"
                android:text="@string/info_internet_check"
                android:textColor="@color/colorPrimary" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/signOutAlert"
                android:background="@color/white"
                android:orientation="horizontal"
                android:weightSum="1">

                <Button
                    android:id="@+id/btn_yes"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dip"
                    android:layout_weight="1"
                    android:background="@color/colorBackground"
                    android:gravity="center"
                    android:minHeight="?attr/actionBarSize"
                    android:text="OK"
                    android:textAppearance="?attr/textAppearanceSearchResultTitle"
                    android:textColor="@color/colorAccent" />


            </LinearLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</FrameLayout>

Hope this may helps :)

custom_bar的主要布局中放置android:gravity="center"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="abc"
            android:textSize="30sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="abc"
            android:textSize="30sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="abc"
            android:textSize="30sp" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Done" />


    </LinearLayout>

</LinearLayout>
------
dialogBuilder.showAsDropDown(dialogView, 50, 400, Gravity.CENTER);
alertDialog.show();
----

Add that line to your code. it will set the popup window to center.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM