安卓开发——控件AlertDialog实现方式,设置下部三个按钮,自定义布局设置.setView(dialogView)样式,控件PopupWindow1常用方法,showAsDropDown构造方法

一.AlertDialog

1.实现方式

 AlertDialog . Builder builder = new AlertDialog . Builder ( context );构建 Dialog 的各种参数
 Builder . setlcon ( int iconld );添加 ICON 
 Builder . setTitle ( CharSequence title );添加标题
 Builder . setMessage ( CharSequence message );添加消息
 Builder . setView ( View view );设置自定义布局
 Builder . create ();创建 Dialog 
 Builder . show ();显示对话框
 setPositiveButton 确定按钮
 setNegativeButton 取消按钮
 setNeutralButton 中间按钮

1.1注意细节写法

public void leoClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher)
                .setTitle("我是IKUN")
                .setMessage("你干嘛~~~")
                .create()
                .show();
    }

后两项create和show必须放后面前三项可以任意调换位置,

create返回的是AlertDialog show在AlertDialog

在activity_main.xml中写下:

   <Button
        android:text="坤坤集合"
        android:onClick="leoClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

显示效果如图:

 1.2设置下部三个按钮

.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNeutralButton("中间", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })

效果如图所示:

 排布根据不同的型号,是不同的

1,3自定义布局设置.setView(dialogView)样式

View dialogView = getLayoutInflater().inflate(R.layout.dialog_view, null);
.setView(dialogView)
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="你干嘛~~鸡你太美"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

设置后效果如下:

 二.控件PopupWindow

1.常用方法

1. setContentView ( View contentView ):设置 PopupWindow 显示的 View
2. showAsDropDown ( View anchor ):相对某个控件的位置(正左下方),无偏移
3.showAsDropDown( View anchor , int xoff , int yoff ):相对某个控件的位置,有偏移
4. setFocusable ( boolean focusable )设置是否获取焦点
5. setBackgroundDrawable ( Drawable background )设置背景
6. dismiss ()关闭弹窗
7. setAnimationStyle ( int animationStyle )设置加载动画
8. setTouchable ( boolean touchable )设置触摸使能
9. setOutsideTouchable ( boolean touchable )设置 PopupWindow 外面的触摸使能

2.popupWindow中提供很多简便构造方法

无参构造

 一个参数的contentView

两个参数的宽和高width,height 

 三个参数的 宽,高width,height 和contentView

 四个参数宽,高width,height ,contentView和focusable

一般都是用三参或者四参的

完整代码:

    <Button
        android:text="弹出PopupWindow"
        android:onClick="leoClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

2.1contentView就是弹窗显示布局

 此处写300是可以的,但是为了让popupWindow刚好包裹住popupView,一般通过以下方式

 完整代码:

public void leoClick(View view) {
                View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);

                PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.showAsDropDown(view);
            }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@mipmap/ic_launcher"
    android:orientation="vertical">


    <Button
        android:id="@+id/btn1"
        android:layout_width="168dp"
        android:layout_height="94dp"
        android:padding="5dp"
        android:text="广西"
        android:textSize="18sp" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="168dp"
        android:layout_height="94dp"
        android:padding="5dp"
        android:text="钦州"
        android:textSize="18sp" />
</LinearLayout>

效果如图:

也可以对两个按钮进行偏移

3.showAsDropDown构造方法

一个参数

 三个参数,其中xoff,yoff表示向x,y轴偏移

实例如下:

popupWindow.showAsDropDown(view,100,100);

 也可以使用如下方法:

popupWindow.showAsDropDown(view,view.getWidth(),-view.getHeight());

 设置偏移效果如下:

 3.1实现点击空白处,退出popupwindow

使用上述:

4. setFocusable ( boolean focusable )设置是否获取焦点

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个在 `onCreateDialog` 方法中使用 `AlertDialog` 并实现透明背景的例子: 1. 首先在 res/layout 目录下创建一个自定义的弹窗布局文件,例如 `dialog_custom.xml`: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/dialog_background" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/dialog_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="标题" android:textSize="18sp" /> <EditText android:id="@+id/dialog_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入内容" android:inputType="text" /> <Button android:id="@+id/dialog_confirm" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确定" /> <Button android:id="@+id/dialog_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> ``` 2. 在 res/drawable 目录下创建一个透明背景的 drawable 文件,例如 `dialog_background.xml`: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#00000000" /> <corners android:radius="8dp" /> </shape> ``` 3. 在 Activity 中实现 `onCreateDialog` 方法,加载自定义的弹窗布局文件并设置透明背景: ```java @Override protected Dialog onCreateDialog(int id) { if (id == DIALOG_CUSTOM) { AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.dialog_custom, null); builder.setView(view); AlertDialog dialog = builder.create(); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置按钮点击事件 Button confirmBtn = view.findViewById(R.id.dialog_confirm); Button cancelBtn = view.findViewById(R.id.dialog_cancel); confirmBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 确定按钮点击事件 dialog.dismiss(); } }); cancelBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 取消按钮点击事件 dialog.dismiss(); } }); return dialog; } return super.onCreateDialog(id); } ``` 以上代码中,我们首先创建一个 `AlertDialog.Builder` 对象,并使用 `LayoutInflater` 加载自定义布局文件 `dialog_custom.xml`。然后将布局文件设置为对话框的视图,并创建 AlertDialog 对象。接着,我们使用 `getWindow()` 方法获取对话框的窗口对象,并使用 `setBackgroundDrawable()` 方法将窗口背景设置为透明色。最后,我们为确定按钮和取消按钮设置点击事件,并返回创建的 AlertDialog 对象。 在需要弹出对话框的时候,可以使用 `showDialog(DIALOG_CUSTOM)` 方法来显示自定义对话框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杪商柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值