如何在 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);
我在这里缺少什么?感谢您的帮助。
图像:
I am trying to create a custom Dialog, and have its content centered, but it is always ending up left-aligned. Here is 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>
And my (relevant) code:
Dialog d = new Dialog(this);
d.setContentView(R.layout.aboutdialog);
d.setTitle(R.string.app_name);
What am I missing here? Thanks for the assistance.
Image:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试编辑为:
a:layout_gravity="center_horizontal|center_vertical"
或
使用Relative_Layout并将线性布局与父级的中心对齐,对于相对的
fill_parent
对于线性布局使用wrap_content
。尽管它在中心与我合作
try to edit to this:
a:layout_gravity="center_horizontal|center_vertical"
OR
use Relative_Layout and align linear layout to center of parent with
fill_parent
for Relativewrap_content
for Linear.although it is worked with me on center
修改保存内容布局的 Dialog 的 ViewGroup (FrameLayout)。我使用静态方法并在 DialogFragments 中的 onActivityCreated() 内调用它:
Modify Dialog's ViewGroup (FrameLayout) which holds your content layout. I use static method and invoke it inside onActivityCreated() in DialogFragments:
您正在执行layout_gravity,这很好,但是由于您对最外面的布局执行了此操作,因此它无法相对于周围的内容定位自身。
我认为将上面的布局包装在另一个布局中就可以了
You're doing layout_gravity which is good, but since you do it to the outmost layout, it can't position itself relative to what's around it.
I think that wrapping the above layout inside another layout would do it
您可以尝试将 LinearLayout layout_gravity 更改为重力吗?请参阅此处解释的差异:Android 上的重力和layout_gravity
Can you try changing your LinearLayout layout_gravity to just gravity? See the difference explained here: Gravity and layout_gravity on Android