当前位置:   article > 正文

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

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的配置。

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


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

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="horizontal"
  8. android:id="@+id/AddAvatar_Dialog_blank"
  9. tools:context="com.example.lenovo.mydemoapp.myAppTools.AddAvatar">
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:orientation="vertical"
  14. android:background="@color/colorWhite"
  15. android:layout_alignParentBottom="true"
  16. android:padding="10dp">
  17. <LinearLayout
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_marginTop="10dp"
  21. android:orientation="horizontal">
  22. <TextView
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:layout_gravity="center"
  26. android:layout_marginLeft="10dp"
  27. android:text="选择添加方式"
  28. android:textColor="@color/colorBlue"
  29. android:textSize="@dimen/BigTextSize" />
  30. </LinearLayout>
  31. <LinearLayout
  32. android:layout_width="match_parent"
  33. android:layout_height="1px"
  34. android:layout_margin="10dp"
  35. android:background="@color/colorBlue"></LinearLayout>
  36. <LinearLayout
  37. android:id="@+id/AddAvatar_Dialog_CameraButton"
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"
  40. android:background="@drawable/button_background_white_change_gray"
  41. android:orientation="horizontal">
  42. <ImageView
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:src="@mipmap/ic_camera" />
  46. <TextView
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_gravity="center"
  50. android:layout_marginLeft="20dp"
  51. android:text="拍摄照片"
  52. android:textColor="@color/colorBlue"
  53. android:textSize="@dimen/BigTextSize" />
  54. </LinearLayout>
  55. <LinearLayout
  56. android:id="@+id/AddAvatar_Dialog_GalleryButton"
  57. android:layout_width="match_parent"
  58. android:layout_height="wrap_content"
  59. android:background="@drawable/button_background_white_change_gray"
  60. android:orientation="horizontal">
  61. <ImageView
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:src="@mipmap/ic_gallery" />
  65. <TextView
  66. android:layout_width="wrap_content"
  67. android:layout_height="wrap_content"
  68. android:layout_gravity="center"
  69. android:layout_marginLeft="20dp"
  70. android:text="在相册中选择"
  71. android:textColor="@color/colorBlue"
  72. android:textSize="@dimen/BigTextSize" />
  73. </LinearLayout>
  74. </LinearLayout>
  75. </RelativeLayout>

预览图:



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


  1. protected void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.activity_add_avatar);
  4. //隐藏标题栏
  5. ActionBar actionbar = getSupportActionBar();
  6. if(actionbar != null){
  7. actionbar.hide();
  8. }
  9. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/225198
推荐阅读
相关标签
  

闽ICP备14008679号