Android Dialog对话框居中

Android Dialog对话框自定义样式居中

LayoutInflater inflater = getLayoutInflater();
   View layout = inflater.inflate(R.layout.dialog,
     (ViewGroup) findViewById(R.id.dialog));

   new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)
     .setPositiveButton("确定", null)
     .setNegativeButton("取消", null).show();

 

 
 

动态代码

		LinearLayout layout = new LinearLayout(this);
		TextView tv = new TextView(this);
		tv.setText("测试测试测\n测试测试测\n测试测试测\n测试测试测");
		LinearLayout.LayoutParams pm = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layout.addView(tv, pm);
		layout.setGravity(Gravity.CENTER);
		new AlertDialog.Builder(this)
		.setTitle("自定义布局")
		.setView(layout)
		.setPositiveButton("确定", null)
		.setNegativeButton("取消", null)
		.show();


dialog标题居中:

		LinearLayout layout = new LinearLayout(this);
		TextView tv = new TextView(this);
		tv.setText("测试测试测\n测试测试测\n测试测试测\n测试测试测");
		LinearLayout.LayoutParams pm = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layout.addView(tv, pm);
		layout.setGravity(Gravity.CENTER);
		
        TextView title = new TextView(this);
        title.setText("提示");
        title.setGravity(Gravity.CENTER);
        
		new AlertDialog.Builder(this)
		.setCustomTitle(title)
		.setView(layout)
		.setPositiveButton("确定", null)
		.setNegativeButton("取消", null)
		.show();


 

 

 

 

 

 

 

 

你可能感兴趣的:(Android Dialog对话框居中)