Dialog是Android提供的各种对话框的基类,和上篇的DialogFragment类似。为什么还要介绍Dialog呢,因为DialogFragment只能运行在Android3.0以上的系统中。虽然现在手机更新的很快,Android系统更新的也很快,但是Android3.0系统以下的用户,还是存在不少的。所以采用Dialog拥有一定的优势。

这篇文章需要实现的是arcgis for android 的地图切换,gis系统一般会为用户提供多种用户的选中,地图切换是必须的。

1.mapswitchDialog

在res的layout中新建android的xml文档。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:background="@color/white"
  5. android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
  6.  
  7. <LinearLayout
  8. android:id="@+id/l1"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:background="@drawable/common_top_layer_with_projection_bg"
  12. android:gravity="center"
  13. android:orientation="horizontal" >
  14.  
  15. <ImageButton
  16. android:id="@+id/imgswichclose"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:clickable="true"
  20. android:src="@drawable/icon_cancel_normal" />
  21.  
  22. <TextView
  23. android:layout_width="fill_parent"
  24. android:layout_height="fill_parent"
  25. android:layout_gravity="center"
  26. android:gravity="right|center"
  27. android:text="@string/switchmap"
  28. android:textColor="@color/black"
  29. android:textSize="12sp" />
  30.  
  31. </LinearLayout>
  32.  
  33. <LinearLayout
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content"
  36. android:layout_below="@id/l1"
  37. android:orientation="horizontal" >
  38.  
  39. <LinearLayout
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_below="@id/l1"
  43. android:gravity="center"
  44. android:orientation="vertical" >
  45.  
  46. <ImageButton
  47. android:id="@+id/imgmap_1"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:src="@drawable/main_map_mode_3d_normal" />
  51.  
  52. <TextView
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:text="@string/smap1"
  56. android:textColor="@color/black"
  57. android:layout_gravity="center"
  58. android:gravity="right|center" />
  59.  
  60. </LinearLayout>
  61.  
  62. <LinearLayout
  63. android:id="@+id/l3"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:layout_marginLeft="20dp"
  67. android:gravity="center"
  68. android:orientation="vertical" >
  69.  
  70. <ImageButton
  71. android:id="@+id/imgmap_2"
  72. android:layout_width="wrap_content"
  73. android:layout_height="wrap_content"
  74. android:src="@drawable/main_map_mode_3d_normal" />
  75.  
  76. <TextView
  77. android:layout_width="wrap_content"
  78. android:layout_height="wrap_content"
  79. android:layout_gravity="center"
  80. android:text="@string/map_2"
  81. android:textColor="@color/black" />
  82.  
  83. </LinearLayout>
  84. </LinearLayout>
  85. </RelativeLayout>

mapdialog

这个xml文档,也就是我们所说的地图切换的布局页面。

2.建立mapSwitchDialog类

在类中,和DialogFragment情况相似,需要实现onCreate()方法。

  1. public class MapswichFragment extends Dialog {
  2.  
  3. private refreshUIListener listenr;
  4. public MapswichFragment(Context context) {
  5. super(context);
  6. // TODO Auto-generated constructor stub
  7. }
  8. public MapswichFragment(Context context,refreshUIListener listener)
  9. {
  10. super(context);
  11. this.listenr=listener;
  12. }
  13. protected void onCreate(Bundle savedInstanceState) {
  14. // TODO Auto-generated method stub
  15. super.onCreate(savedInstanceState);
  16. this.setContentView(R.layout.mapswitch_fragment);
  17. ImageView image1=(ImageView)findViewById(R.id.imgswichclose);
  18. image1.setOnClickListener(new View.OnClickListener() {
  19.  
  20. public void onClick(View v) {
  21. // TODO Auto-generated method stub
  22. dismiss();
  23. }
  24. });
  25. ImageButton img1=(ImageButton)findViewById(R.id.imgmap_1);
  26. img1.setOnClickListener(new View.OnClickListener() {
  27.  
  28. public void onClick(View v) {
  29. // TODO Auto-generated method stub
  30. listenr.refreshUi();
  31.  
  32. }
  33.  
  34. });
  35. }
  36. public interface refreshUIListener
  37. {
  38. public void refreshUi();
  39. }
  40.  
  41. }

Dialog class

在oncreate方法中,为该dialog指定页面。需要强调的是,在进行地图切换的时候,地图需要实时的在手机上进行显示,也就是我们点击dialog中的图片按钮,Activity要进行更新。在网上看到别人的解决方法还不错,通过定义接口的方法来实现。定义一个事件监听的接口,并在接口中定义一个方法,在构造函数中初始化该监听,在事件中调用该方法。

3.main.xml

main.xml是主页面,这里就不多说了,需要添加一个mapview和一个button。mapview用来显示地图,button用来切换地图。

button的监听事件中调用刚刚定义的dialog就可以实现地图切换。

  1. ImageButton imgswitch=(ImageButton)findViewById(R.id.btnmapswitch);
  2. imgswitch.setOnClickListener(new View.OnClickListener() {
  3.  
  4. @Override
  5. public void onClick(View v) {
  6. // TODO Auto-generated method stub
  7. MapswichFragment frag=new MapswichFragment(MainMapActivity.this,new MapswichFragment.refreshUIListener() {
  8.  
  9. @Override
  10. public void refreshUi() {
  11. // TODO Auto-generated method stub
  12. mMapView.removeAll();
  13. ArcGISTiledMapServiceLayer titleLayer=new ArcGISTiledMapServiceLayer(layerurl);
  14. mMapView.addLayer(titleLayer);
  15. mMapView.addLayer(graphcisLayer);
  16. }
  17. });
  18. frag.requestWindowFeature(Window.FEATURE_NO_TITLE);
  19. frag.show();
  20. }
  21. });

地图切换

4.dialog.style

通过style设置dialog的样式。

  1. <style name="dialog" parent="@android:style/Theme.Dialog">
  2. <item name="android:windowFrame">@null</item><!--边框-->
  3. <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
  4. <item name="android:windowIsTranslucent">false</item><!--半透明-->
  5. <item name="android:windowNoTitle">true</item><!--无标题-->
  6. <item name="android:windowBackground">@color/transparent</item><!--背景透明-->
  7. <item name="android:backgroundDimEnabled">false</item><!--模糊-->
  8. </style>

定义dialog样式

5.定义dialog位置

通过windowmanager设置dialog的显示位置。

  1. Window dialogWindow=frag.getWindow();
  2. WindowManager.LayoutParams lg= dialogWindow.getAttributes();
  3.  
  4. dialogWindow.setGravity(Gravity.RIGHT|Gravity.TOP);
  5. lg.y=90;
  6. lg.x=20;
  7. lg.height=android.view.WindowManager.LayoutParams.WRAP_CONTENT;
  8. lg.width=(int)(getWindowManager().getDefaultDisplay().getWidth());
  9. dialogWindow.setAttributes(lg);
  10.  
  11. frag.setCanceledOnTouchOutside(true);

dialog的位置

Android学习自定义Dialog的更多相关文章

  1. Android创建自定义dialog方法详解-样式去掉阴影效果

    在自定义组件时,从已有组件源码中会很大收获.就拿progressDialog来说     间接父类是dialog,想了解dialog继承结构可以去百度,或者    从构造器来说ProgressDial ...

  2. Android:自定义Dialog

    自定义Dialog:显示SeekBar 效果图: 步骤: //SettingActivity.java button4.setOnClickListener(new View.OnClickListe ...

  3. Android学习之Dialog

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框; 实例如下: 1.效果图: 2.XML代码: ...

  4. 【Android】自定义Dialog

    先上图 main.xml主界面文件 <?xml version="1.0" encoding="utf-8"?><LinearLayout x ...

  5. Android学习笔记-Dialog详解

    1.对话框的使用 1.1AlertDialog的显示 简单对话框以及监听的设置:重点掌握三个按钮(也就是三上单词): PositiveButton(确认按钮);NeutralButton(忽略按钮) ...

  6. Android如何自定义dialog

    ; window.setAttributes(lp); // set the confirm button if (positiveButtonClickListener != null) { ((B ...

  7. Android学习----自定义Adapter实现ListView

    前言: 对于ListView而言,自定义的Adapter对于显示复杂的界面有很大的灵活性 .使用自定义的Adapter需要继承BaseAdapter,然后重写getCount(),getView(), ...

  8. Android开发之自定义Dialog简单实现

    本文着重研究了自定义对话框,通过一下步骤即可清晰的理解原理,通过更改界面设置和style类型,可以应用在各种各样适合自己的App中. 首先来看一下效果图: 首先是activity的界面 点击了上述图片 ...

  9. 【转】 Pro Android学习笔记(四四):Dialog(1):触发Dialog

    目录(?)[-] 创建dialog fragment Activity显示对话框 Android提供alert.prompt.pick-list,单选.多选,progress.time-picker和 ...

随机推荐

  1. Atom编辑器

    Atom介绍 Github的员工Nathan Sobo在Atom的博客中提到:”Sublime和TextMate十分方便,但是扩展性不足:另一方面,Emacs和 Vim扩展性很强却需要学习日程工作中很 ...

  2. linux下Mysql 的安装、配置、数据导入导出

    MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),虽然功能未必很强大,但因它的免费开源而广受欢迎. 这次,接着上一篇<CentOs minimal安装和开发环境部署>,讲下L ...

  3. TCP协议三次握手

    TCP协议三次握手过程分析 TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: ...

  4. Andriod定时任务

    参考地址:http://blog.sina.com.cn/s/blog_73288dd10101m6xs.html,http://blog.csdn.net/fancsxx/article/detai ...

  5. asp.net后台的一些操作

    1.在后台绑定下拉框再返回到前台 protected StringBuilder sq = new StringBuilder();//为了在前台绑定 protected void Page_Load ...

  6. 使用react-native做一个简单的应用-03欢迎界面

    Android和iOS的欢迎界面是不一样的,在iOS中有一个默认的欢迎界面,而Android则需要自己写.因此我就分开说一下这两个平台的欢迎界面的搭建.下面先看一下实现效果: Android: iOS ...

  7. java字符串数组进行大小排序

    若是将两个字符串直接比较大小,会包:The operator > is undefined for the argument type(s) java.lang.String, java.lan ...

  8. 动态链接库的生成(dll)和 动态链接库隐式and显式调用

    一.构建动态链接库(dll.dll dll.lib dll.h) 说明: .dll 是在执行程序是调用  .lib 是在连接程序是调用  .h是在编译程序时调用 1.头文件(声明导入函数):_decl ...

  9. CSS 怀疑 Verify

    1. height 之前好像看到有人说,div标签的height设置一个固定值后,div标签的height会随着内容的增多而变大 经测试,内容会溢出div标签,但div标签的height不会随着变化

  10. Python之路第八天,进阶-设计模式

    设计模式 单例模式 单例,顾名思义单个实例. 学习单例之前,首先来回顾下面向对象的内容: python的面向对象由两个非常重要的两个"东西"组成:类.实例 面向对象场景一: 如:创 ...