如何在Android对话框中居中布局?
我正在尝试创建一个自定义对话框,并将其内容居中,但始终以左对齐结束。 这里是aboutdialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:a="http://schemas.android.com/apk/res/android"
  a:orientation="vertical"
  a:id="@+id/AboutDialogLayout" 
  a:layout_width="fill_parent" 
  a:layout_height="fill_parent" 
  a:layout_gravity="center_horizontal" 
  a:gravity="center_horizontal">
    <ImageView a:id="@+id/imageView1" 
               a:src="@drawable/pricebook" 
               a:layout_height="wrap_content" 
               a:layout_width="wrap_content"/>
    <TextView a:layout_height="wrap_content"         
              a:textAppearance="?android:attr/textAppearanceLarge" 
              a:id="@+id/textView1" 
              a:text="Price Book" 
              a:layout_width="wrap_content"/>
    <TextView a:layout_height="wrap_content" 
              a:id="@+id/textView2" 
              a:layout_width="wrap_content" 
              a:text="foo"/>
</LinearLayout>
和我的(相关)代码:
Dialog d = new Dialog(this);
d.setContentView(R.layout.aboutdialog);
d.setTitle(R.string.app_name);
我在这里错过了什么? 感谢您的帮助。
  图片: 
  尝试编辑为: a:layout_gravity="center_horizontal|center_vertical" 
  要么 
  使用Relative_Layout并使用fill_parent将线性布局与父fill_parent中心对齐,以获得Linear的相对wrap_content 。 
虽然它在我的中心工作
修改保存您的内容布局的对话框的ViewGroup(FrameLayout)。 我使用静态方法并在DialogFragments中的onActivityCreated()中调用它:
/**
 * Center dialog content inside available space
 * @param dialog
 */
public static void centerDialogContent(Dialog dialog) {
ViewGroup decorView = (ViewGroup) dialog.getWindow().getDecorView();
View content = decorView.getChildAt(0);
FrameLayout.LayoutParams contentParams = (FrameLayout.LayoutParams)content.getLayoutParams();
contentParams.gravity = Gravity.CENTER;
content.setLayoutParams(contentParams);
}
你正在做layout_gravity,这是很好的,但是因为你做到了最外层的布局,所以它不能把它自己定位在与它相关的东西上。
我认为将上述布局封装在另一个布局中会做到这一点
链接地址: http://www.djcxy.com/p/93319.html