这个实现功能花了一点时间,当时忙了很晚,只怪当时没有想出其他解决办法。言归正传。
前几天有这么一个小伙伴,在开发有这样的地图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();
}
});
}
}
更多内容,请关注公众号