效果图

前言

首先说说为什么需要大费周章的去用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. Oracle存储过程和自定义函数笔记

    学习地址:https://www.imooc.com/learn/370 存储过程和存储函数定义:指存储在数据库中供所有用户程序调用的子程序叫做存储过程 .存储函数. 相同点:完成特定功能的程序.区别 ...

  2. 读取配置文件的C语言接口实现

    在一些场合,需要对一些配置文件进行读取,去设置软件的参数,自己实现了一些接口函数,以供以后使用. ConfigFile.c #include <stdio.h> #include < ...

  3. 【java】构造函数

    什么时候自定义构造函数:当分析事物时,事物一存在就具备一些特征或者行为,那么就把这么内容定义在构造函数中 作用:对对象进行初始化,对象一建立,就会自动调用与之对应的构造函数 特点: 1.函数名和类名相 ...

  4. sqlalchemy操作----多表关联

    有二张表,一张作者表,一张书表,一个作者写多本书,一本书可以由多个作者写,与是通过新加一张关系表把他们联系起来 #!/usr/bin/env python # -*- coding: utf-8 -* ...

  5. NAND FLASH控制器

    一.nand flash访问原理 地址空间概念 nand的编址 nand命令  命令,地址,数据 使用S3C2440的nand flash控制器访问nand flash 前几个编译出来的文件都小于4k ...

  6. excel技巧--复制带excel侧边的表格

    假设要复制出带excel侧边的表格内容,则使用以下步骤: 1.选择要复制的表格: 2.选择“页面布局”->“标题”选项的“打印”打勾: 3.选择“开始”->复制旁的三角选项:复制为图片: ...

  7. ALGO-119_蓝桥杯_算法训练_寂寞的数

    问题描述 道德经曰:一生二,二生三,三生万物. 对于任意正整数n,我们定义d(n)的值为为n加上组成n的各个数字的和.例如,d()=++=, d()=++++=. 因此,给定了任意一个n作为起点,你可 ...

  8. 【redis】之centos6.x安装redis3.0.x

    centos6.9_x86_64 1.下载redis安装包 http://download.redis.io/releases/redis-3.2.9.tar.gz 2.解压 编译到指定得目录 mak ...

  9. ApplicationDomain

    ApplicationDomain 类的用途是存储 ActionScript 3.0 定义表.SWF 文件中的所有代码被定义为存在于ApplicationDomain 中.在使用 Loader 类 A ...

  10. bzoj5045: 打砖块

    Description 小Q最近沉迷于一款新型<打砖块>游戏.在每局游戏中,呈现在屏幕上的是一堵无限大小的墙壁.墙壁上镶嵌着 无数长度为2.宽度为1的砖块.墙壁被分成若干行,每行宽度都为1 ...