AlertDialog实现方式,设置下部三个按钮

一.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 )    设置是否获取焦点

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AlertDialog通常使用setPositiveButton()和setNegativeButton()方法来设置对话框的按钮。这些按钮默认是在对话框底部排列,且宽度相等。 如果你想要自定义按钮的位置和宽度,可以使用setView()方法来自定义AlertDialog的布局。你可以创建一个布局文件,然后在其中添加两个按钮,并设置它们的位置和宽度。然后在AlertDialog中使用setView()方法将该布局文件设置为对话框的视图。 以下是一个示例布局文件,其中包含两个按钮,一个在左侧,一个在右侧,并且它们的宽度不同: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/left_button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="Left" /> <Button android:id="@+id/right_button" android:layout_width="0dp" android:layout_weight="2" android:layout_height="wrap_content" android:text="Right" /> </LinearLayout> ``` 然后在代码中,你可以使用以下方法来设置AlertDialog的视图,并将其中的按钮设置为自定义布局中的按钮: ``` AlertDialog.Builder builder = new AlertDialog.Builder(context); View customLayout = LayoutInflater.from(context).inflate(R.layout.custom_layout, null); builder.setView(customLayout); Button leftButton = customLayout.findViewById(R.id.left_button); Button rightButton = customLayout.findViewById(R.id.right_button); leftButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Do something when left button is clicked } }); rightButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Do something when right button is clicked } }); AlertDialog dialog = builder.create(); dialog.show(); ``` 这样,你就可以自定义AlertDialog按钮位置和宽度了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值