1. 程式人生 > >Android開發(AlertDialog對話方塊自定義佈局和多選列表不共存的替代辦法)

Android開發(AlertDialog對話方塊自定義佈局和多選列表不共存的替代辦法)

這個實現功能花了一點時間,當時忙了很晚,只怪當時沒有想出其他解決辦法。言歸正傳。

前幾天有這麼一個小夥伴,在開發有這樣的地圖app,該地圖app有多個地圖圖層,這些地圖圖層可提供給使用者操作,比如說圖層的顯示控制,以及選擇需要的圖層供查詢。由於該地圖app在主介面已經佈局很多按鈕實現其他功能,所以再加上該圖層控制按鈕就沒有存放的位置了,所以該小夥伴設計出一個彈出框(alertdialog)來控制圖層的顯示。但是在建立對話方塊,他設計了一個自定義佈局(有滑動條,用於控制圖層透明度)和多選(用於圖層控制),具體如下圖所示。

那麼現在問題來了,多選倒是在對話方塊中顯示了,而自定義佈局沒有顯示,於是他好像花了很多時間去解決這問題,沒有找到原因,又不想自定義帶有列表(listview)對話方塊。於是找我幫忙,當時也是挺晚了,於是我看了一下程式碼,因為當時急需,所以還是花大量時間去自定義佈局,重寫了介面卡,和回撥列表操作。

今天又來研究了一下,其實沒有必要那麼麻煩的,想想也不用自定義列表佈局,也可以讓他的那個自定義佈局顯示。可以使用自定義頭部佈局來顯示。具體的展示介面如上圖設計介面。

好了來貼一下自定義的頭部佈局程式碼。以及在主activity中的實現程式碼。是不是覺得很簡單。哈哈。

自定義程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_weight="6"
    android:layout_marginTop="10px"
    android:layout_marginLeft="10px"
    android:layout_marginRight="10px"
    android:background="@color/colorPrimaryDark"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <TextView
        android:layout_width="match_parent"
        android:layout_marginLeft="10dp"
        android:textColor="#fff"
        android:layout_weight="4"
        android:textSize="15dp"
        android:layout_height="wrap_content"
        android:text="@string/str_layerchoose" />
    <TextView
        android:layout_width="2dp"
        android:background="#fff"
        android:layout_height="wrap_content" />
    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_weight="2"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50" />


</LinearLayout>

 活動程式碼,非常簡單。

package com.example.qin.alertdialogtest;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;



public class MainActivity extends AppCompatActivity {
    public Button btn_dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_dialog=findViewById(R.id.btn_dialog);
        btn_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                View layout = inflater.inflate(R.layout.layer_choose, null);

                final String[] items = new String[] { "圖層測繪","圖層地理資訊系統","圖層遙感科學與技術","圖層貂蟬", "圖層西施", "圖層主管", "圖層設計", "圖層開發","圖層土豆","圖層南瓜","圖層香蕉",
                "圖層webgis","圖層arcgis","圖層openlayer","圖層stampgis","圖層supermap","圖層gps"};

                boolean[] checkedArray = new boolean[items.length];
                for (int i=0;i<items.length;i++){
                    checkedArray[i]=false;
                }

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                builder.setCustomTitle(layout);


                builder.setMultiChoiceItems(items, checkedArray, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                    }
                });
                builder.create().show();


            }
        });
    }
}

                                                                                  更多內容,請關注公眾號