效果图
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packageName}.${activityClass}" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="弹出Dialog" /> </RelativeLayout>
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv_dialog" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
listview_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="姓名" android:textSize="18sp" /> <TextView android:id="@+id/age" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="年龄" android:textSize="18sp" /> </LinearLayout>
MyCustomDialog.java
package com.example.mydialog; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Dialog; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.widget.ListView; import android.widget.SimpleAdapter; public class MyCustomDialog extends Dialog { public MyCustomDialog(Context context) { super(context); // TODO Auto-generated constructor stub } public MyCustomDialog(Context context, int theme) { super(context); // TODO Auto-generated constructor stub } public static class Builder { private Context context; private ListView listView; private String[] names = new String[] { "张三", "李四", "王五" }; private String[] ages = new String[] { "10", "20", "30" }; public Builder(Context context) { this.context = context; } public MyCustomDialog create() { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); final MyCustomDialog customDialog = new MyCustomDialog(context); View view = inflater.inflate(R.layout.custom_dialog, null); listView = (ListView) view.findViewById(R.id.lv_dialog); LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //不显示标题栏 customDialog.addContentView(view, layoutParams); bindAdapter(); customDialog.setContentView(view); return customDialog; } private void bindAdapter() { SimpleAdapter adapter = new SimpleAdapter(context, getData(), R.layout.listview_item, new String[] { "name", "age" }, new int[] { R.id.name, R.id.age }); listView.setAdapter(adapter); } private List<HashMap<String, String>> getData() { List<HashMap<String, String>> maps = new ArrayList<HashMap<String, String>>(); for (int i = 0; i < names.length; i++) { HashMap<String, String> map = new HashMap<String, String>(); map.put("name", names[i]); map.put("age", ages[i]); maps.add(map); } return maps; } } }
MainActivity.java
package com.example.mydialog; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private MyCustomDialog.Builder dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button1); dialog = new MyCustomDialog.Builder(this); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.create().show(); } }); } }