如何在 Android 对话框中居中布局?

发布于 2024-12-03 16:50:00 字数 1259 浏览 3 评论 0原文

我正在尝试创建一个自定义对话框,并将其内容居中,但它总是以左对齐结束。这是 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:
Screen shot of Dialog problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷路的信 2024-12-10 16:50:00

尝试编辑为:a:layout_gravity="center_horizo​​ntal|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 Relative wrap_content for Linear.

although it is worked with me on center

失与倦" 2024-12-10 16:50:00

修改保存内容布局的 Dialog 的 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);
}

Modify Dialog's ViewGroup (FrameLayout) which holds your content layout. I use static method and invoke it inside onActivityCreated() in DialogFragments:

/**
 * 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);
}
情愿 2024-12-10 16:50:00

您正在执行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

血之狂魔 2024-12-10 16:50:00

您可以尝试将 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文