📌  相关文章
📜  如何在Android中更改AlertDialog的位置?

📅  最后修改于: 2021-05-08 20:36:24             🧑  作者: Mango

android中的AlertDialog是UI窗口小部件之一,可立即弹出以确认用户交互或确认用户执行的操作。在大多数应用程序中,警报对话框的位置在中心。在本文中,已经讨论了如何在android中更改警报对话框的位置。请看下图,以区分带有中心位置的常规警报对话框和具有更改位置的警报对话框。注意,我们将使用Java语言实现该项目。

在Android中更改AlertDialog的位置

在不同位置实施警报对话框的步骤

步骤1:创建一个空的活动项目

  • 创建一个空的活动android studio项目。并选择Java作为编程语言。
  • 参考Android |如何在Android Studio中创建/启动新项目?知道如何使用空的活动android studio项目创建一个项目。

步骤2:使用activity_main.xml文件

  • 该应用程序的主布局包含一个按钮,该按钮用于在指定位置构建显示警报对话框。
  • activity_main.xml文件中调用以下代码以实现UI。
XML


  
    


Java
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
  
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the button with it's appropriate ID
        Button bShowAlertDialog = findViewById(R.id.showAlertDialogButton);
  
        // handle the SHOW ALERT DIALOG BUTTON
        bShowAlertDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                // instance of alert dialog to build alert dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.logo);
                builder.setTitle("This is Alert Dialog");
                builder.setMessage("Bottom Alert dialog");
  
                // set the neutral button to do some actions
                builder.setNeutralButton("DISMISS", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "Alert Dialog Dismissed", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // set the positive button to do some actions
                builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "OKAY", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // set the negative button to do some actions
                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "CANCEL", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // show the alert dialog
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
  
                // get the top most window of the android 
                  // screen using getWindow() method
                // and set the the gravity of the window to 
                  // top that is alert dialog will be now at
                // the topmost position
                alertDialog.getWindow().setGravity(Gravity.TOP);
            }
        });
    }
}


Java
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
  
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the button with it's appropriate ID
        Button bShowAlertDialog = findViewById(R.id.showAlertDialogButton);
  
        // handle the SHOW ALERT DIALOG BUTTON
        bShowAlertDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                // instance of alert dialog to build alert dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.logo);
                builder.setTitle("This is Alert Dialog");
                builder.setMessage("Bottom Alert dialog");
  
                // set the neutral button to do some actions
                builder.setNeutralButton("DISMISS", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "Alert Dialog Dismissed", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // set the positive button to do some actions
                builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "OKAY", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // set the negative button to do some actions
                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "CANCEL", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // show the alert dialog
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
  
                // get the top most window of the android 
                  // scree using getWindow() method
                // and set the the gravity of the window to 
                  // top that is alert dialog will be now at
                // the bottom position
                alertDialog.getWindow().setGravity(Gravity.BOTTOM);
            }
        });
    }
}


输出界面:

在Android中更改AlertDialog的位置

步骤3:使用MainActivity。 Java文件

要更改Android窗口顶部的警报对话框位置,请调用以下代码以实现。添加了注释以便更好地理解。

Java

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
  
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the button with it's appropriate ID
        Button bShowAlertDialog = findViewById(R.id.showAlertDialogButton);
  
        // handle the SHOW ALERT DIALOG BUTTON
        bShowAlertDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                // instance of alert dialog to build alert dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.logo);
                builder.setTitle("This is Alert Dialog");
                builder.setMessage("Bottom Alert dialog");
  
                // set the neutral button to do some actions
                builder.setNeutralButton("DISMISS", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "Alert Dialog Dismissed", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // set the positive button to do some actions
                builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "OKAY", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // set the negative button to do some actions
                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "CANCEL", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // show the alert dialog
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
  
                // get the top most window of the android 
                  // screen using getWindow() method
                // and set the the gravity of the window to 
                  // top that is alert dialog will be now at
                // the topmost position
                alertDialog.getWindow().setGravity(Gravity.TOP);
            }
        });
    }
}

输出:在模拟器上运行

要更改Android窗口底部的警报对话框位置,请调用以下代码以实现。添加了注释以便更好地理解。

Java

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
  
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the button with it's appropriate ID
        Button bShowAlertDialog = findViewById(R.id.showAlertDialogButton);
  
        // handle the SHOW ALERT DIALOG BUTTON
        bShowAlertDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                // instance of alert dialog to build alert dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.logo);
                builder.setTitle("This is Alert Dialog");
                builder.setMessage("Bottom Alert dialog");
  
                // set the neutral button to do some actions
                builder.setNeutralButton("DISMISS", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "Alert Dialog Dismissed", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // set the positive button to do some actions
                builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "OKAY", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // set the negative button to do some actions
                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "CANCEL", Toast.LENGTH_SHORT).show();
                    }
                });
  
                // show the alert dialog
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
  
                // get the top most window of the android 
                  // scree using getWindow() method
                // and set the the gravity of the window to 
                  // top that is alert dialog will be now at
                // the bottom position
                alertDialog.getWindow().setGravity(Gravity.BOTTOM);
            }
        });
    }
}

输出:在模拟器上运行

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!