Android开发之利用Activity实现Dialog对话框
作者:daisy 发布时间:2022-12-25 21:13:00
标签:android,activity,dialog
前言
在Android中经常要使用Dialog来实现一些提示以及一些特殊的效果,而且样式也不一样,每次都得查一大堆资料,还不一定能解决。对话框是个好东西,创建简单有实用。当下的开发中,很多的开发者反而更喜欢使用activity来代替对话框,至少笔者的团队中,类似于升级提示或者指示页及其他一些交互的地方,大量的把Dialog替换成activity,好处是显而易见的,activity具有更灵活的操作和布局,另外很重要一点是,一些容易涉及内存泄漏的代码放在activity中执行比放在Dialog中执行要好的多,当然这是笔者自己的观点,文中有不对的地方,欢迎大家提出指正,好让笔者及时改正,共同学习。
先上效果图:
实现方法
这个对话框常常能在一些APP中遇到,首先是布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="300dp"
android:layout_height="400dp"
android:layout_gravity="center"
android:background="@drawable/popup_bg"
android:orientation="vertical"
tools:context="com.yankee.september_2.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_update"
android:layout_marginBottom="24dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Version"
android:textColor="#53BAF8"
android:textSize="14sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nowadays, when it comes to the issues of robots, individuals' opinions vary from person to person。 Some people believe that robots will enlighten our life, while other are worried about that they will ruin the whole world。 As I see, the increasing number of robots has the power to alter everything in the coming future entirely。"
android:textColor="#404040"
android:textSize="12sp" />
</ScrollView>
</LinearLayout>
<Button
android:id="@+id/btn_update"
android:layout_width="130dp"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="Update now"
android:textColor="#FFFFFF"
android:textSize="14sp" />
</RelativeLayout>
布局代码中需要注意的是,这个布局的大小是写死的,因为从代码中可以看出,对话框的镂空效果其实是一张背景图产生的,辛苦美术但是爽了程序员。
第二步:接着就是在manifest文件的操作了,把对应的activity的theme设置为自定义的主题即可,背景设为透明,去掉标题,注意,这个主题继承自对话框的主题。
manifest:
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/TipDialog">
</activity>
styles.xml:
<style name="TipDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
最后就没有最后了,还有一点,就是让当前这个activity继承自Activity,而不是AppCompatActivity,否则会报主题的错误。
总结


猜你喜欢
- Mybatis的缓存mybatis是一个查询数据库的封装框架,主要是封装提供灵活的增删改sql,开发中,service层能够通过mybati
- 一、为什么要控制当你在项目启动时需要提前做一个业务的初始化工作时,或者你正在开发某个中间件需要完成自动装配时。你会声明自己的Configur
- 前言介绍最近很多同学找我帮忙做一些课程设计或Web前端大作业、其中控制台项目应该是初学者必须经历的一个过程、作为java初学者这个控制台版本
- 在项目中用到了MapStruct,对其可以转换JavaBean特别好奇,因为之前都是使用Vo的方式手动set转换,但是接触到MapStruc
- 传统方式克隆羊问题现在有一只羊 tom,姓名为: tom,年龄为:1,颜色为:白色,请编写程序创建和 tom羊属性完全相同的10只羊。传统方
- 使用了Android的系统API实现了多点触控功能,多点触控对设备的硬件有一定的要求,目前市面上的手机几乎都能实现多点触控了。实现多点触控最
- Java进阶之FileUpload完成上传的实例 FileUpload是Apache commons下面
- import java.util.HashMap;import java.util.Map;import org.apache.common
- 本文实例为大家分享了Android百度地图定位、显示用户当前位置的工具类,供大家参考,具体内容如下1、构建定位Option的工具类impor
- 一.EventBus概述 1.EventBus的三要素EventBus有三个主要的元素需要我们先了解一下:Event:事件,可以是任意类型的
- Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullP
- 代码如下:using System; using System.Collections.Generic; using S
- 一、前言想要自定义starter组件,首先要了解springboot是如何加载starter的,也就是springboot的自动装配机制原理
- 利用Java连接MySQL做登陆界面,供大家参考,具体内容如下1、首先需要建立一个类,在这里,我命名为newLoginnewLogin类的代
- 本文实例为大家分享了C++实现哈夫曼编码的具体代码,供大家参考,具体内容如下#include<iostream>#include
- C语言运算符及其优先级汇总表口诀圆下箭头一顿号非凡增减富强针地长三乘除,四加减,五移位千万别把鱼忘记,它在盛饭的厨子里小灯大灯灯灯不等爸喂鱼
- 最近有个需求,需要统计APP的在线人数,其实以前也统计过,采取的是上线发送一个请求$this->cache->incr()加1,
- 项目结构:pom.xml文件: <parent>
- 我们知道android是基于Looper消息循环的系统,我们通过Handler向Looper包含的MessageQueue投递Message
- 序列化是将一个对象转换成字节流以达到将其长期保存在内存、数据库或文件中的处理过程。它的主要目的是保存对象的状态以便以后需要的时候使用。与其相