如何在Android Dialog内部布局?

Con*_*rad 12 android dialog android-layout customdialog

我正在尝试创建一个自定义对话框,并将其内容置于中心位置,但它始终以左对齐方式结束.这是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>
Run Code Online (Sandbox Code Playgroud)

我的(相关)代码:

Dialog d = new Dialog(this);
d.setContentView(R.layout.aboutdialog);
d.setTitle(R.string.app_name);
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?谢谢你的帮助.

图片: 对话框问题的屏幕截图

Ale*_*lex 11

尝试编辑到这个:a:layout_gravity="center_horizontal|center_vertical"

使用Relative_Layout并将线性布局与父级中心对齐fill_parent,wrap_content为线性相对.

虽然它在我的中心工作

  • 将整个XML包装在RelativeLayout中就可以了.两个*布局的`layout_width'必须是`fill_parent`,并且'layout_height'都需要设置为`wrap_content`(至少这是我想要的). (3认同)