效果图

前言

首先说说为什么需要大费周章的去用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. golang gopacket网络抓包和分析

    gopacket 是golang语言使用的网络数据抓取和分析的工具包. 本文简单介绍如何使用gopacket进行网络抓包. 下载gopacket # go get git@github.com:goo ...

  2. Hanlp中使用纯JAVA实现CRF分词

    Hanlp中使用纯JAVA实现CRF分词 与基于隐马尔可夫模型的最短路径分词.N-最短路径分词相比,基于条件随机场(CRF)的分词对未登录词有更好的支持.本文(HanLP)使用纯Java实现CRF模型 ...

  3. centos7 tomcat自启动

    第一步: vim /lib/systemd/system/tomcat.service [Unit] Description=tomcat After=network.target [Service] ...

  4. centos 6.X 安装nodejs v6.11.0和npm

    下载nodejs wget -c https://nodejs.org/dist/v6.11.0/node-v6.11.0-linux-x64.tar.xz 安装gcc++ yum install - ...

  5. SDI视频采集过程

    SDI视频采集过程 GTP收发模块为视频采集系统的核心部分,包含发送和接收,完成对信号的解串和串码.并且HD-SDI信号中并非所有的信号都是有效视频信号,这部分功能由数据分析模块实现,并将提取出来的有 ...

  6. TextBox限制输入字母、数字、退格键

    公共方法如下: /// <summary> /// 正则表达式验证只能输入数字或字母 /// </summary> /// <param name="pendi ...

  7. Java内部类的一些注意事项

    背景:最近在做一个项目,为了保证前台风格的统一,前台选用的是GWT框架.GWT通过回调的方式向后台取得数据,在前台展示,因此很多的赋值操作只能在回调函数中通过set方法来实现.我的目的是从后台读取一个 ...

  8. IE10 下系统出现Unable to get property 'PageRequestManager' of undefined or null reference错误

    在本地调试时没有任何问题,上传到测试服务器(win2003 framework 4.0)后打开网站出现Unable to get property 'PageRequestManager' of un ...

  9. Spring @Async实现异步调用示例

    什么是“异步调用”? “异步调用”对应的是“同步调用”,同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执行:异步调用指程序在顺序执行时,不等待异步调用的语句返回结果 ...

  10. 学习笔记之Gurobi

    Gurobi Optimization - The State-of-the-Art Mathematical Programming Solver http://www.gurobi.com/ind ...