自定义Dialog显示布局,位置

时间:2022-11-01 11:04:28

布局item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="@color/lagoumain_listview_item_pre"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:gravity="center"
        android:text="学历"
        />


</LinearLayout>

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:scrollbars="none"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@color/msg_invite_filter_state_line0"
        android:dividerHeight="0.3dip"
        android:listSelector="@drawable/listview_item_bg"
        ></ListView>

</RelativeLayout>



样式

 <style name="Dialog" parent="android:style/Theme.Dialog">
        <item name="android:background">#00000000</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
    </style>


代码

 private void dialogEducation(){

        final AlertDialog builder = new AlertDialog.Builder(this,R.style.Dialog).create(); // 先得到构造器
        builder.show();
        builder.getWindow().setContentView(R.layout.dialog_jianli);
        LayoutInflater factory = LayoutInflater.from(this);
        View view = factory.inflate(R.layout.dialog_jianli, null);
        builder.getWindow().setContentView(view);
        listview= (ListView) view.findViewById(R.id.lv_dialog);
        StringAdapter adapter;
       
            adapter=new StringAdapter(education,this);
       
        listview.setAdapter(adapter);
        Window dialogWindow = builder.getWindow();
        dialogWindow.setGravity( Gravity.BOTTOM);//显示在底部
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.height = (int) (d.getHeight() * 0.5); // 高度设置为屏幕的0.5
        p.width = d.getWidth(); // 宽度设置为屏幕宽
        dialogWindow.setAttributes(p);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                
                    tv_education.setText(education[position]);
                
                builder.dismiss();
            }
        });
    }

解决自定义Dialog不能弹出软件盘

 builder.getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);


自定义Dialog显示布局,位置