效果图

前言

首先说说为什么需要大费周章的去用activity实现一个dialog,明明android系统已经提供了一个更方便的dialog了。原因如下:

1.activity模式的dialog可以实现更多的功能,比如activity的值传入与传出,生命周期的使用等等。这个是一个dialog无法具备的功能。

2.这样一个activity有的对话框就算可以实现很多功能,用那些地方可以实现运用环境呢?举例一个,在头像设置或者图片选择的情况下可以使用这种模式的对话框,因为头像设置如果在很多地方有大量需要复用,在这个需要使用头像设置的activity下面就需要复写启动相机或者启动相册的代码,而将activity作为对话框(这对话框activity里写入启动相机或者相册的代码),我们就可以反复调用它。因为我们可以使用Intent传入关键的值在分别获取我们想要的图片。

3.在一些不在前台的情况下,启动一个对话框(比如在service里..)

实现思维

1.首先我们需要在styles.xml 写一个适用与dialog形式的theme

2.在AndroidManifest.xml,添加我们写的theme的配置。

3.然后是创建activity,并且写入自己想要的xml布局。

4.有时候,你已经在AndroidManifest.xml里面隐藏了标题栏,但是这个时候在sytles.xml的隐藏标题栏没有作用,我们需要在代码上隐藏标题栏。

1.首先我们需要在styles.xml 写一个适用与dialog形式的theme

<style name="dialogStyle" parent="AppTheme">
<!--android:windowBackground 设置背景透明-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--android:windowNoTitle 设置没有标题-->
<item name="android:windowNoTitle">true</item>
<!--android:windowIsFloating 设置浮在最上面 -->
<item name="android:windowIsFloating">true</item>
<!--android:windowIsTranslucent 设置窗口是半透明的-->
<item name="android:windowIsTranslucent">true</item>
<!--android:windowContentOverlay 设置窗口内容叠加 是否有覆盖-->
<item name="android:windowContentOverlay">@null</item>
<!--android:windowAnimationStyle 设置弹出动画-->
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<!--android:backgroundDimEnabled 设置背景是否模糊 -->
<item name="android:backgroundDimEnabled">true</item>
</style>

2.然后是创建activity,并且在AndroidManifest.xml,添加我们写的theme

 <activity
android:name=".myAppTools.AddAvatar"
android:theme="@style/MyToolsAddAvatar" />

3.写入自己想要的xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/AddAvatar_Dialog_blank"
tools:context="com.example.lenovo.mydemoapp.myAppTools.AddAvatar"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorWhite"
android:layout_alignParentBottom="true"
android:padding="10dp"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:text="选择添加方式"
android:textColor="@color/colorBlue"
android:textSize="@dimen/BigTextSize" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_margin="10dp"
android:background="@color/colorBlue"></LinearLayout> <LinearLayout
android:id="@+id/AddAvatar_Dialog_CameraButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background_white_change_gray"
android:orientation="horizontal"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_camera" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:text="拍摄照片"
android:textColor="@color/colorBlue"
android:textSize="@dimen/BigTextSize" />
</LinearLayout> <LinearLayout
android:id="@+id/AddAvatar_Dialog_GalleryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background_white_change_gray"
android:orientation="horizontal"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_gallery" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:text="在相册中选择"
android:textColor="@color/colorBlue"
android:textSize="@dimen/BigTextSize" />
</LinearLayout> </LinearLayout> </RelativeLayout>

预览图:

4.有时候,你已经在AndroidManifest.xml里面隐藏了标题栏,但是这个时候在sytles.xml的隐藏标题栏没有作用,我们需要在代码上隐藏标题栏。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_avatar);
//也可以用下面的办法,在代码里隐藏标题栏
ActionBar actionbar = getSupportActionBar();
if(actionbar != null){
actionbar.hide();
}
}

android 开发 实现一个activity变成dialog对话框的更多相关文章

  1. android开发中一个activity如何调用另一个xml中的控件

    有时候,我们需要在一个activity中使用另一个activity中的控件,这时候就不能直接findViewById,不然会报错指向空对象,这时就需要像下面这样做. LayoutInflater fa ...

  2. Android开发(19)---常见dialog对话框的运用

    Dialog是android开发过程中最常用到的组件之一,Dialog的创立办法有两种: 一是直接new一个Dialog目标,然后调用Dialog目标的show和dismiss办法来操控对话框的显现和 ...

  3. Android Activity作为dialog对话框的使用详细介绍

    Activity做为Android应用层四大组件的重要组成部分,它的灵活性.可扩性无论是在应用还是游戏方面都得到了广泛应用,本文主要介绍Activity作为dialog对话框 的使用方法进行说明. 本 ...

  4. android开发中的 Activity 与 Context 区别与联系

    Context 是 Application /Activity /Service的基类 Intent(Context , Class); Activity中的上下文Context是随着活动的产生而产生 ...

  5. Android开发工程师文集-Activity生命周期,启动方式,Intent相关介绍,Activity详细讲解

    前言 大家好,给大家带来Android开发工程师文集-Activity生命周期,启动方式,Intent相关介绍,Activity详细讲解的概述,希望你们喜欢 Activity是什么 作为一个Activ ...

  6. Android开发之入口Activity

    Android开发之入口Activity Adnroid App是怎样确定入口Activity的? 难道就由于class的类名叫MainActivity,布局文件叫activity_main.xml? ...

  7. Android世界第一个activity启动过程

    Android世界第一个activity启动过程 第一次使用Markdown,感觉不错. Android系统从按下开机键一直到launcher的出现,是一个如何的过程,中间都做出了什么操作呢.带着这些 ...

  8. Android开发中在一个Activity中关闭另一个Activity

    比如有ActivityA, ActivityB,在ActivityB中关闭ActivityA 解决方案: 1. 在 ActivityA 里面设置一个静态的变量instance,初始化为this在 Ac ...

  9. Android学习(十九)Dialog对话框

    一.什么是Dialog对话框 对话框是当前页面中弹出的一个小窗口,用于显示重要的提示信息,提示用户输入信息,确认信息,或者显示某种状态,如下载进度,退出提示等等.用户需要与对话框进行交互,才能回到原窗 ...

随机推荐

  1. show master status, show slave status中的Executed_Gtid_Set

    slave 如果server是slave节点,在server上执行show master staus与show slave status显示的Executed_Gtid_Set是一样的. slave也 ...

  2. C#:decimal的去0显示

    public static string DecimalToString(decimal d) { return d.ToString("#0.######"); } 后面的0会给 ...

  3. Java消息机制 ActiveMQ入门实例

    转载自:http://www.cnblogs.com/wyh3721/p/5917316.html 1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/  ...

  4. 让邮件服务器发出的Email不被认为是垃圾邮件

    配置一个Email服务器很简单,用Postfix等软件稍微配置一下就可以了,几分钟搞定. 但Email服务器发出去的Email很可能经常被人当成垃圾邮件,怎么配置Email服务器才能让Email服务器 ...

  5. MS Batch AI

    微软的Batch AI服务是一项新服务,它可以帮助你在GPU pool上训练和测试机器学习模型,包括深度学习模型.它简化了在当前许多流行的深度学习框架(如TensorFlow.Microsoft认知工 ...

  6. ASP防止SQL注入

    '防止SQL注入'http://0.0.0.0/bzhs/login.asp?logType=edit';WAITFOR DELAY '0:0:5' --logType = Replace(Repla ...

  7. spring IOC中四种依赖注入方式

    在spring ioc中有三种依赖注入,分别是:https://blog.csdn.net/u010800201/article/details/72674420 a.接口注入:b.setter方法注 ...

  8. VS2013编译Qt5.2.1 32位静态库debug-and-release版及结果分享

    1. 下载zip源码,我下载的是qt-everywhere-opensource-src-5.2.1.zip这个文件. 2.安装python 3.解压缩qt-everywhere-opensource ...

  9. webGL之three.js入门1

    开场白 最近开始学前端,看了极客学院的前端教学视频,其实有C++或者java基础的人学前端还是很快的.但是html的标签和CSS的样式还是得多code才能熟练,熟能生巧,学以致用. 还在看js,因为有 ...

  10. 黄聪:is_file和file_exists效率比较

    目前在弄文件缓存的时候用到了判定文件存在与否,is_file()还是file_exists()呢?is_file和file_exists两者效率比较起来,谁的运行速度更快呢?还是做个测试吧: 1 2 ...