iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >android自定义对话框实例代码
  • 185
分享到

android自定义对话框实例代码

2024-04-02 19:04:59 185人浏览 独家记忆
摘要

1.实现效果    2.定义dialog.xml (res/layout/dialog.xml) <?xml version="1.0" encoding="utf

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对话框内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: android自定义对话框实例代码

本文链接: https://www.lsjlt.com/news/161223.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作