android - 自定义具有单选列表项的对话框

标签 android android-alertdialog

我创建了一个包含单选列表项的对话框:

final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Colors");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            }
        });
AlertDialog alert = builder.create();
alert.show();

如何自定义此对话框的布局,以便对话框中的每个列表项都包含一个图标和一个文本。如何为对话框中的列表创建自定义布局?

最佳答案

创建自定义对话框的步骤:

  1. 创建对话框布局文件,如:

    <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content">
    <!-- The Title Bar -->
    <LinearLayout
        android:id="@+id/dlg_priority_titlebar"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">
    
        <ImageView
            android:src="@drawable/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dip" />
    
        <TextView
            android:layout_width = "wrap_content"
                android:layout_height = "wrap_content"
                android:text = "Select Task Priority"
                android:layout_gravity = "center_vertical" />
    </LinearLayout>
    <ListView 
        android:id="@+id/dlg_priority_lvw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dlg_priority_titlebar"
        android:background="@drawable/layout_home_bg">
    </ListView>
    

  2. 因为ListView中的布局是自定义的,所以要为ListView创建一个布局文件:

    <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 
        android:orientation = "horizontal" 
        android:layout_width = "fill_parent" 
        android:layout_height = "fill_parent"> 
    
    <ImageView 
          android:id = "@+id/list_priority_img" 
          android:layout_width = "wrap_content" 
          android:layout_height = "wrap_content" 
          android:layout_gravity = "center_vertical" 
          android:layout_margin = "5dip" /> 
    <TextView 
         android:id = "@+id/list_priority_value" 
         android:layout_width = "wrap_content" 
         android:layout_height = "wrap_content" 
         android:layout_gravity = "center_vertical" 
         android:textsize = "28dip" 
         android:textColor = "@drawable/ black" /> 
    </LinearLayout>
    
  3. 创建自定义 DialogPriorityDlg 继承自 Dialog

         public class PriorityDlg extends Dialog {
    
        private Context context;
        private ListView dlg_priority_lvw = null;
        public PriorityDlg(Context context) {
            super(context);
            this.context = context;
            // TODO Auto-generated constructor stub
        }
        public PriorityDlg(Context context, int theme) {
            super(context, theme);
            this.context = context;
        }
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            this.setContentView(R.layout.dlg_priority);
            dlg_priority_lvw = (ListView) findViewById(R.id.dlg_priority_lvw);
            // ListView
            SimpleAdapter adapter = new SimpleAdapter(context, getPriorityList(),
                    R.layout.lvw_priority, new String[] { "list_priority_img",
                            "list_priority_value" }, new int[] {
                            R.id.list_priority_img, R.id.list_priority_value });
            dlg_priority_lvw.setAdapter(adapter);
            //ListView
            dlg_priority_lvw
                    .setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int arg2, long arg3) {
    
                        }
                    });
        }
        private List<HashMap<String, Object>> getPriorityList() {
            List<HashMap<String, Object>> priorityList = new ArrayList<HashMap<String, Object>>();
            HashMap<String, Object> map1 = new HashMap<String, Object>();
            map1.put("list_priority_img", R.drawable.priority_not_important);
            map1.put("list_priority_value", context.getResources().getString(
                    R.string.dlg_priority_not_important));
            priorityList.add(map1);
            HashMap<String, Object> map2 = new HashMap<String, Object>();
            map2.put("list_priority_img", R.drawable.priority_general);
            map2.put("list_priority_value", context.getResources().getString(
                    R.string.dlg_priority_general));
            priorityList.add(map2);
            HashMap<String, Object> map3 = new HashMap<String, Object>();
            map3.put("list_priority_img", R.drawable.priority_important);
            map3.put("list_priority_value", context.getResources().getString(
                    R.string.dlg_priority_important));
            priorityList.add(map3);
            HashMap<String, Object> map4 = new HashMap<String, Object>();
            map4.put("list_priority_img", R.drawable.priority_very_important);
            map4.put("list_priority_value", context.getResources().getString(
                    R.string.dlg_priority_very_important));
            priorityList.add(map4);
    
            return priorityList;
        }
    
    }
    
  4. 创建自定义对话框

    PriorityDlg dlg = new PriorityDlg (SimpleTaskActivity.this, R.style.dlg_priority); 
    dlg.show();
    

其中R.style.dlg_priority设置对话框使用样式文件,只是让对话框去掉标题栏,当然你可以通过代码来完成这个效果:

<? Xml version = "1.0" encoding = "utf-8"?> 
<resources> 
    <style name="dlg_priority" parent="@android:Theme.Dialog"> 
        <item name = "android: windowNoTitle"> true </ item> 
    </ style> 
</ resources>

关于android - 自定义具有单选列表项的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9839395/

相关文章:

android - 应用已在 Google Play 上发布,但无法使用 Google 语音操作

java - onResume 后 Activity 未重新绑定(bind)到前台服务

android - 使用 DialogInterface 的 onClick() 方法

Android AlertDialog 标题字体

android - 使用来自 Firebase 控制台的特定数据填充 ListView

android - appcompat v22.1 小部件着色不显示 5.0 之前的版本

android - 在应用程序启动时绑定(bind)并启动服务

android - 如何检测我的 AlertDialog 被系统杀死?

java - 从 Android 中的 AlertDialog 返回信息

android - Android 4.1 上的 AlertDialog 问题