robotMsgDialog = new Dialog(this); robotMsgDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); Window window = robotMsgDialog.getWindow(); window.setGravity(Gravity.CENTER); window.setBackgroundDrawable(new BitmapDrawable()); robotMsgDialog.setCanceledOnTouchOutside(true); robotMsgDialog.show();
两种方式设置dialog的宽高
1.设置宽
WindowManager.LayoutParams params = robotMsgDialog.getWindow().getAttributes(); params.width = (int) (this.getWindowManager().getDefaultDisplay().getWidth() * 0.7); robotMsgDialog.getWindow().setAttributes(params); 2.设置宽高
WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context .WINDOW_SERVICE); int width = (int) (windowManager.getDefaultDisplay().getWidth() * 0.7); int height = (int) (windowManager.getDefaultDisplay().getHeight() * 0.3); window.setLayout(width, height);
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_robotmsg, null); robotMsgDialog.addContentView(dialogView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams .WRAP_CONTENT)); dialog_ListView = (ListView) dialogView.findViewById(R.id.dialog_listView); myAdapter = new RobotHistoryMsgAdapter(getApplicationContext()); myAdapter.setData(list); dialog_ListView.setAdapter(myAdapter);
dialog布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:gravity="center"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:background="@drawable/dialog_round_bg_shape" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:paddingBottom="5dp" android:paddingLeft="16dp" android:paddingTop="10dp" android:text="历史连接" android:textColor="#000" android:textSize="20sp" android:textStyle="bold"/> <View android:layout_width="match_parent" android:layout_height="1.25dp" android:background="#ccc"/> <ListView android:id="@+id/dialog_listView" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout> </RelativeLayout>