android 开发 实现一个activity变成dialog对话框

效果图:

    

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

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

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



实现思维:

1.首先我们需要在styles.xml 写一个activity的配置。

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

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

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



1.首先我们需要在styles.xml 写一个activity的配置。

<style name="MyToolsAddAvatar" parent="AppTheme">
        <!--背景色,此处的背景色请一定要设置为透明度背景色-->
        <item name="android:windowBackground">@color/transparent</item>
        <!--window Is Translucent 窗口是半透明的-->
        <item name="android:windowIsTranslucent">true</item>
        <!--window No Title窗口无标题-->
        <item name="android:windowNoTitle">true</item>
        <!--弹出动画-->
        <item name="android:windowAnimationStyle">@null</item>
    </style>


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

 <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();
        }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值