android自定义对话框实例代码

 更新时间:2021年12月30日 09:58:14   作者:扒鸡撕坦  
大家好,本篇文章主要讲的是android自定义对话框实例代码,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览

1.实现效果

  

2.定义dialog.xml (res/layout/dialog.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <RelativeLayout
        android:layout_width="250sp"
        android:layout_height="270sp"
        android:layout_centerInParent="true">
 
        <TextView
            android:id="@+id/dialog_title"
            android:layout_width="wrap_content"
            android:layout_height="35sp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15sp"
            android:textColor="#333333"
            android:textSize="17sp" />
 
        <TextView
            android:id="@+id/dialog_message"
            android:layout_width="wrap_content"
            android:layout_height="160sp"
            android:layout_centerInParent="true"
            android:layout_marginTop="65sp"
            android:textColor="#333333"
            android:textSize="17sp" />
 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30sp"
            android:layout_marginBottom="20sp"
            android:orientation="horizontal">
 
            <LinearLayout
                android:id="@+id/dialog_confirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
 
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/confirm_button_style"
                    android:gravity="center"
                    android:text="确定"
                    android:textColor="@color/white"
                    android:textSize="18sp" />
            </LinearLayout>
 
            <LinearLayout
                android:id="@+id/dialog_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="22sp">
 
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cancel_button_style"
                    android:gravity="center"
                    android:text="取消"
                    android:textColor="@color/teal_200"
                    android:textSize="18sp" />
            </LinearLayout>
        </LinearLayout>
 
    </RelativeLayout>
</RelativeLayout>

3. 设置确定、取消按钮的background

上文的dialog.xml中,确定和取消按钮都是TextView,所以需要自定义按钮的背景

confirm_button_style.xml  (所有的color需要自定义)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="1000sp"/>
    <solid android:color="@color/teal_200"/>
    <stroke
        android:width="0.5sp"
        android:color="@color/colorAccent"/>
    <size android:width="105sp" android:height="40sp"/>
</shape>

cancel_button_style.xml 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="1000sp"/>
    <solid android:color="@color/white"/>
    <stroke
        android:width="0.5sp"
        android:color="@color/teal_200"/>
    <size android:width="105sp" android:height="40sp"/>
</shape>

4. 自定义dialog的使用

final AlertDialog dialog = new AlertDialog.Builder(xxxClass.this).create();
dialog.setCancelable(false); //点击对话框以外的位置,不消失
dialog.show();
 
Window window = dialog.getWindow();
window.setContentView(R.layout.dialog);
//标题
TextView title = window.findViewById(R.id.dialog_title);
title.setText("dialog_title");
 
//内容
TextView message = window.findViewById(R.id.dialog_message);
message.setText("dialog_message ");
 
//确定按钮
LinearLayout confirm = window.findViewById(R.id.dialog_confirm);
confirm.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //xxx
    }
});
 
//取消按钮
LinearLayout cancel = window.findViewById(R.id.dialog_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //xxx
    }
});

到此这篇关于android自定义对话框实例代码的文章就介绍到这了,更多相关android对话框内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于Android实现一个常用的布局吸顶效果

    基于Android实现一个常用的布局吸顶效果

    这篇文章给大家介绍一个布局吸顶效果,一般出现在内容较长页面还嵌套着分类页面的情况,比如电商的详情页嵌套分类,在页面滑动到tab的时候我们希望tab还能保留在页面顶部而不被顶上去,文中有详细的代码示例,需要的朋友可以参考下
    2023-09-09
  • Android编程四大组件分别是什么

    Android编程四大组件分别是什么

    大家知道Android编程四大组件分别是什么吗?这篇文章主要为大家详细介绍了Android四大组件,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • android虚拟键盘弹出遮挡登陆按钮问题的解决方法

    android虚拟键盘弹出遮挡登陆按钮问题的解决方法

    这篇文章主要介绍了android虚拟键盘弹出遮挡登陆按钮问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Kotlin扩展方法超详细介绍

    Kotlin扩展方法超详细介绍

    Kotlin 可以为一个不能修改的或来自第三方库中的类编写一个新的函数。 这个新增的函数就像那个原始类本来就有的函数一样,可以用普通的方法调用,这种机制的函数称为扩展函数
    2022-09-09
  • Android Popupwindow弹出窗口的简单使用方法

    Android Popupwindow弹出窗口的简单使用方法

    这篇文章主要为大家详细介绍了Android Popupwindow弹出窗口的简单使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Android使用DrawerLayout仿QQ6.6版侧滑效果

    Android使用DrawerLayout仿QQ6.6版侧滑效果

    这篇文章主要为大家详细介绍了Android使用DrawerLayout仿QQ6.6版侧滑效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android使用TextView,设置onClick属性无效的解决方法

    Android使用TextView,设置onClick属性无效的解决方法

    下面小编就为大家带来一篇Android使用TextView,设置onClick属性无效的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • Android TextView预渲染研究

    Android TextView预渲染研究

    这篇文章主要介绍了Android TextView预渲染研究的相关资料,需要的朋友可以参考下
    2016-09-09
  • Android编程基于距离传感器控制手机屏幕熄灭的方法详解

    Android编程基于距离传感器控制手机屏幕熄灭的方法详解

    这篇文章主要介绍了Android编程基于距离传感器控制手机屏幕熄灭的方法,结合具体实例形式分析了Android距离传感器的控制屏幕熄灭的实现方法与相关操作技巧,需要的朋友可以参考下
    2017-11-11
  • Android实现自定义圆形进度条

    Android实现自定义圆形进度条

    这篇文章主要介绍了Android自定义圆形进度条实现代码,进度条在Android中教程经常使用到,本文向大家分享了Android实现自定义圆形进度条的代码,感兴趣的小伙伴们可以参考一下
    2016-03-03

最新评论