Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自定義 Dialog 實現地圖切換

編輯:關於 Android 程式設計

Dialog 是 Android 為各種對話框提供的基類,和上一篇的 DialogFragment 類似。那既然有了 DialogFragment,為何還要介紹 Dialog 呢?原因很現實:DialogFragment 只能運行在 Android 3.0 以上的系統中。如今手機與系統的更新固然飛快,但 Android 3.0 以下的用戶仍不在少數,所以在兼容性上,Dialog 依然佔有一定優勢。

本文要實現的,是 ArcGIS for Android 的地圖切換。GIS 系統通常會為用戶提供多種地圖底圖,地圖切換也就成了必不可少的的功能。

1. 地圖切換布局 mapswitch.xml

在 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>

2. 建立 MapswichFragment 類

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

3. 主界面調用

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 在實際項目中的典型玩法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved