編輯:關於 Android 程式設計
Dialog 是 Android 為各種對話框提供的基類,和上一篇的 DialogFragment 類似。那既然有了 DialogFragment,為何還要介紹 Dialog 呢?原因很現實:DialogFragment 只能運行在 Android 3.0 以上的系統中。如今手機與系統的更新固然飛快,但 Android 3.0 以下的用戶仍不在少數,所以在兼容性上,Dialog 依然佔有一定優勢。
本文要實現的,是 ArcGIS for Android 的地圖切換。GIS 系統通常會為用戶提供多種地圖底圖,地圖切換也就成了必不可少的的功能。
在 res 的 layout 中新建 XML 文檔,包含一個標題欄(含關閉按鈕)和兩個地圖選項(各含一個 ImageButton 與 TextView):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical">
<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: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:layout_gravity="center"
android:text="@string/smap1"
android:textColor="@color/black" />
</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>
與 DialogFragment 情況相似,需要重寫 onCreate() 方法。關鍵在於:地圖切換時,Activity 需要實時刷新顯示,也就是點擊 dialog 中的圖片按鈕後,Activity 要隨之更新。這裡借鑑了一種不錯的思路——通過定義接口的方法來實現:定義一個事件監聽接口,在接口中聲明一個方法,於構造函數中初始化該監聽,並在點擊事件中調用這個方法。
public class MapswichFragment extends Dialog {
private refreshUIListener listenr;
public MapswichFragment(Context context) {
super(context);
}
public MapswichFragment(Context context, refreshUIListener listener) {
super(context);
this.listenr = listener;
}
protected void onCreate(Bundle savedInstanceState) {
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) {
dismiss(); // 關閉 dialog
}
});
ImageButton img1 = (ImageButton)findViewById(R.id.imgmap_1);
img1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
listenr.refreshUi(); // 回調刷新 Activity 的地圖
}
});
}
// 事件監聽接口
public interface refreshUIListener {
public void refreshUi();
}
}
main.xml 是主頁面,需要添加一個 MapView(顯示地圖)和一個 Button(打開切換 dialog)。在 button 的監聽事件中調用剛才定義的 dialog,並在接口實現裡完成地圖的切換刷新:
ImageButton imgswitch = (ImageButton)findViewById(R.id.btnmapswitch);
imgswitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MapswichFragment frag = new MapswichFragment(MainMapActivity.this, new MapswichFragment.refreshUIListener() {
@Override
public void refreshUi() {
mMapView.removeAll();
ArcGISTiledMapServiceLayer titleLayer = new ArcGISTiledMapServiceLayer(layerurl);
mMapView.addLayer(titleLayer);
mMapView.addLayer(graphcisLayer);
}
});
frag.requestWindowFeature(Window.FEATURE_NO_TITLE);
frag.show();
}
});
通過「Dialog + 接口回調」的組合,我們既實現了自定義樣式的地圖切換面板,又讓 Activity 能即時響應切換、刷新底圖——這正是自定義 Dialog 在實際項目中的典型玩法。
Android 自訂 ActionSheet 底部彈出選單實作
先看成品: 實作步驟 一、主畫面配置 只有一顆「退出系統」按鈕,用 RelativeLayout 靠頂對齊即可。 <RelativeLayout xmlns:a
Android 傳感器入門(二):方向傳感器與指南針實作
本節引言 上一節我們學過傳感器的基本概念與通用使用套路,本節接著談方向傳感器的用法。 一、三維座標系的概念 Android 平台的傳感器框架通常以一組標準的三維座
Android 開發入門:打造你的第一個 App
完成開發環境的建置之後,接下來就是實際動手打造自己的第一個應用程式了。本文帶您快速熟悉整個專案的目錄結構,為後續的開發打好基礎。 建立新專案 在 Eclipse 中選擇建
Android 粒子爆炸特效:從原始碼到可擴展架構的重構實錄
簡介 前陣子隨手亂逛時看到一款粒子爆炸特效的開源控件,效果確實好看,但原始碼的擴展性不太理想——想換一種爆炸風格,得改動的地方非常多。 於是動手做了一輪重構,核心思路只有