项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

示例如下:

/view/flyout/AlertDialogDemo3.java

/**
 * AlertDialog 大小、位置和动画
 */

package com.webabcd.androiddemo.view.flyout;

import android.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.webabcd.androiddemo.R;

public class AlertDialogDemo3 extends AppCompatActivity {

    private Button mButton1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_flyout_alertdialogdemo3);

        mButton1 = findViewById(R.id.button1);

        sample();
    }

    private void sample() {
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogDemo3.this).setMessage("内容").create();
                alertDialog.show();

                Window dialogWindow = alertDialog.getWindow();

                // 设置对话框的 Gravity 位置
                dialogWindow.setGravity(Gravity.CENTER);

                // 设置对话框的位置(这里的 x, y 位置是相对于 Gravity 的位置),大小和透明度
                WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
                layoutParams.x = 100;
                layoutParams.y = 100;
                layoutParams.width = 300;
                layoutParams.height = 300;
                layoutParams.alpha = 0.3f;
                dialogWindow.setAttributes(layoutParams);

                // 设置对话框的显示和隐藏动画(参见 res/values/styles.xml 中的 MyAlertDialogAnimationStyle)
                dialogWindow.setWindowAnimations(R.style.MyAlertDialogAnimationStyle);
            }
        });
    }
}

/layout/activity_view_flyout_alertdialogdemo3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="弹出一个指定大小和位置,以及具有自定义动画效果的 AlertDialog" />

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

相关文章:

  • 2021-05-30
  • 2021-11-04
  • 2021-09-26
  • 2021-06-30
  • 2022-03-06
  • 2022-03-08
  • 2021-11-22
  • 2021-11-07
猜你喜欢
  • 2021-12-08
  • 2021-12-06
  • 2021-09-09
  • 2021-05-16
  • 2021-06-12
  • 2022-02-14
  • 2021-09-11
相关资源
相似解决方案