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

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

1.mapswitchDialog

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

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout
android:id="@+id/l1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/common_top_layer_with_projection_bg"
android:gravity="center"
android:orientation="horizontal" > <ImageButton
android:id="@+id/imgswichclose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/icon_cancel_normal" /> <TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="right|center"
android:text="@string/switchmap"
android:textColor="@color/black"
android:textSize="12sp" /> </LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/l1"
android:orientation="horizontal" > <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/l1"
android:gravity="center"
android:orientation="vertical" > <ImageButton
android:id="@+id/imgmap_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/main_map_mode_3d_normal" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/smap1"
android:textColor="@color/black"
android:layout_gravity="center"
android:gravity="right|center" /> </LinearLayout> <LinearLayout
android:id="@+id/l3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center"
android:orientation="vertical" > <ImageButton
android:id="@+id/imgmap_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/main_map_mode_3d_normal" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/map_2"
android:textColor="@color/black" /> </LinearLayout>
</LinearLayout>
</RelativeLayout>

mapdialog

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

2.建立mapSwitchDialog类

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

 public class MapswichFragment extends Dialog {

     private refreshUIListener listenr;
public MapswichFragment(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MapswichFragment(Context context,refreshUIListener listener)
{
super(context);
this.listenr=listener;
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.mapswitch_fragment);
ImageView image1=(ImageView)findViewById(R.id.imgswichclose);
image1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
dismiss();
}
});
ImageButton img1=(ImageButton)findViewById(R.id.imgmap_1);
img1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
listenr.refreshUi(); } });
}
public interface refreshUIListener
{
public void refreshUi();
} }

Dialog class

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

3.main.xml

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

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

  ImageButton imgswitch=(ImageButton)findViewById(R.id.btnmapswitch);
imgswitch.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
MapswichFragment frag=new MapswichFragment(MainMapActivity.this,new MapswichFragment.refreshUIListener() { @Override
public void refreshUi() {
// TODO Auto-generated method stub
mMapView.removeAll();
ArcGISTiledMapServiceLayer titleLayer=new ArcGISTiledMapServiceLayer(layerurl);
mMapView.addLayer(titleLayer);
mMapView.addLayer(graphcisLayer);
}
});
frag.requestWindowFeature(Window.FEATURE_NO_TITLE);
frag.show();
}
});

地图切换

4.dialog.style

通过style设置dialog的样式。

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

定义dialog样式

5.定义dialog位置

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

     Window dialogWindow=frag.getWindow();
WindowManager.LayoutParams lg= dialogWindow.getAttributes(); dialogWindow.setGravity(Gravity.RIGHT|Gravity.TOP);
lg.y=90;
lg.x=20;
lg.height=android.view.WindowManager.LayoutParams.WRAP_CONTENT;
lg.width=(int)(getWindowManager().getDefaultDisplay().getWidth());
dialogWindow.setAttributes(lg); 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. LFS:kernel panic VFS: Unable to mount root fs

    说明: 使用Vm虚拟机构建自己的LFS系统时,系统引导不成功,提示 kernel panic VFS: Unable to mount root fs 参考链接:http://www.52os.net ...

  2. Hadoop: HDFS 格式化时,出现 “ERROR namenode.NameNode: java.io.IOException: Cannot create directory /usr/hadoop/tmp/dfs/name/current”

    原因是 没有设置 /usr/hadoop/tmp 的权限没有设置, 将之改为: chown –R hadoop:hadoop /usr/hadoop/tmp 查看:

  3. Android studio教程:[5]活动的生命周期

    想要学好安卓开发,就必须理解安卓软件的生命周期,明白一个活动的创建.启动.停止.暂停.重启和销毁的过程,知道各个阶段会调用什么函数进行处理不同的情况,这里我就通过一个简单的例子让大家明白一个活动的生命 ...

  4. 使用SignalR和SQLTableDependency跟踪数据库中记录的变动

    原文地址:查看 SqlTableDependency是一个组件用来接收数据库的通知,包含在数据表上该记录的值的Inserted.Deleted或者Update操作. 备注:原文提供示例代码下载,但是j ...

  5. Java路径问题最终解决方案—可定位所有资源的相对路径寻址

    1.在Java项目中,应该通过绝对路径访问文件,以下为访问的常用方法: 第一种方法:类名.class.getResource("/").getPath()+文件名 第二种方法:Th ...

  6. EntityFramework+Autofac+MVC+EasyUI 搭建公司基本服务项目

    去年年底用了2个星期的时间搭建了 这套框架,从ORM,IOC,MVC,EasyUI都仔细的研究了下,还有日志,异常等等.mvc 我已经开发过很多项目了,我就觉得自己写套框架,看看自己的水平如何.欢迎大 ...

  7. 正式学习 react(三)

    有了基础的webpack基础,我们要对react的基本语法进行学习. 我这个教程全部用es6 实现.可能会忽略一些最基本的语法讲解,这些你在官网上或者其他别的地方都比我讲的全. 今天我要讲一下reac ...

  8. android activity启动的时候隐藏软键盘

    1.概述 android如果界面有EditText之类的跳软键盘的控件  在跳转到该界面是默认会跳出软键盘的. 更何况有些需求要直接需要获取焦点 <requestFocus /> 如果是E ...

  9. android的注意点

    1.使用Message.callback Message msg = Message.obtain(myThreadHandler,new Runnable() { @Override public  ...

  10. 【额 原来ms sqlserver 中的视图果然是“虚表”哈】

    在视图中查询数据的时候,会不会使用实体表中的列上的索引呢?会 .... 测试结果 测试脚本 ; BEGIN INSERT INTO Teachers ( TeacherName, Sex, Money ...