自定义Dialog

150 阅读1分钟

内容部分是个listview
在这里插入图片描述
1创建Dialog

 private void getJobTitleDialog() {

        View dialogView = getLayoutInflater().inflate(R.layout.dialog_view, null);
        lvJobTitle = (ListView) dialogView.findViewById(R.id.lv_job_title);
        if(records!=null){
            lvJobTitle.setAdapter(new MyAdapter(records,this));
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        TextView title = new TextView(this);
        title.setTextSize(20);
        title.setTextColor(Color.BLACK);
        title.setGravity(Gravity.CENTER);
        title.setText("职务");
        builder.setCustomTitle(title)
                .setView(dialogView)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e("wy", "onClick: 点击了确定" );
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e("wy", "onClick: 点击了取消" );
                    }
                })
                .create()
                .show();

    }

2 dialog_view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="391dp"
    android:layout_height="695dp"
    android:orientation="vertical"
    >
    <TextView
        android:background="@color/stroke_grey"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>
    <ListView
        android:id="@+id/lv_job_title"
        android:divider="@null"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>

</LinearLayout>