zoukankan      html  css  js  c++  java
  • 一手遮天 Android

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

    一手遮天 Android - view(弹出类): AlertDialog 大小、位置和动画

    示例如下:

    /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:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="弹出一个指定大小和位置,以及具有自定义动画效果的 AlertDialog" />
    
    </LinearLayout>
    
    

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

  • 相关阅读:
    C# 程序打包
    [置顶] 我的iOS作品
    struts2处理上传文件路径问题
    Lua基础 函数(二)
    JS解析XML的实现代码
    软件架构设计之Utility模块——Any
    详解Java解析XML的四种方法
    JS解析XML
    Android中级第八讲安卓子线程,以及定时任务使用讲解
    关于"未能映射路径"问题
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_view_flyout_AlertDialogDemo3.html
Copyright © 2011-2022 走看看