Dialog自定义对话框

本文介绍了如何自定义对话框,包括新建对话框布局、设置风格、建立自定义Dialog类,以及在MainActivity中调用和添加适配器的案例。
摘要由CSDN通过智能技术生成

一、自定义对话框

实现步骤:

1.自定义一个类,继承于Dialog类,在构造方法中调用setContentView(R.layout.xx)来设定对话框的布局 

2.设定自定义对话框的风格(不显示标题栏,不显示背景),由于继承了Dialog,默认添加了Dialog标题的一栏 

3.找到res-->values-->styles.xml设置风格 

4.调用含设定对话框风格参数的构造 

5.对自定义对话框中的某些控件添加事件 

6.实例化自定义的对话框,显示


一、新建对话框的布局文件(即弹出的对话框的样子)

layout.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="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="#FFFF00"
    android:padding="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提示"
        android:textSize="34sp"
        android:textStyle="bold"
        android:layout_marginTop="30dp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#ffffff"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="您确定要退出当前程序么"
        android:layout_marginTop="10dp"
        android:textSize="20dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal">
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="否"
            android:onClick="onClick"
            />
        <Button
            android:id="@+id/btn2"
            android:layout_width="
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值