简体   繁体   English

Android AlertDialog无法垂直居中/缩放

[英]Android AlertDialog not centering/scaling vertically

I'm building an AlertDialog that contains a custom View like so: 我正在构建一个包含自定义视图的AlertDialog,如下所示:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_INTRO:
            // setup the dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            View inflatedLayout = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.intro, null);
            builder.setView(inflatedLayout);

            // return the dialog
            return builder.create();
    }

    return super.onCreateDialog(id);
}

And the layout XML: 以及布局XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:background="#000000">

    <TableLayout 
        android:layout_height="match_parent" 
        android:layout_width="match_parent"
        android:stretchColumns="2"
        android:shrinkColumns="1">

        <TableRow>
            <TextView 
                android:id="@+id/intro_TextView_title"
                android:layout_height="wrap_content"
                android:layout_span="2"
                android:gravity="center"
                android:text="@string/intro_title"
                android:textSize="22sp" 
                android:textColor="#FFFFFF" />
        </TableRow>

        <TableRow
            android:layout_marginTop="20dip" >
            <ImageView 
                android:layout_height="wrap_content" 
                android:layout_width="wrap_content"
                android:layout_marginLeft="25dip"
                android:layout_marginRight="25dip"
                android:paddingTop="2dip"
                android:src="@drawable/intro_add" />
            <TextView 
                android:id="@+id/intro_TextView_add"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_marginRight="20dip"
                android:text="@string/intro_add" 
                android:textColor="#FFFFFF" />
        </TableRow>

        <!-- ... -->

    </TableLayout>
</ScrollView>

That gives me a dialog that looks something like this (details blurred, sorry): 这给了我一个看起来像这样的对话框(细节模糊,抱歉):

模糊的AlertDialog屏幕截图

The problem is that big gap at the top (above the dialog). 问题是顶部(对话框上方)的差距很大。 Is there anything I can do to either get rid of it or make the bottom have a similar gap? 我有什么办法可以摆脱它或让底部有类似的缺口?

The bug is described here: http://code.google.com/p/android/issues/detail?id=11824 . 该错误的描述如下: http : //code.google.com/p/android/issues/detail?id=11824 The AlertDialog is reserving space for the title/icon panel even where there is neither a title nor an icon. 即使没有标题也没有图标,AlertDialog仍为标题/图标面板保留空间。

The fix is, I think, quite simple: it should set the top panel layout to GONE, just as it does for the button panel in the event of there being no buttons. 我认为解决方法很简单:应该将顶部面板的布局设置为GONE,就像在没有按钮的情况下对按钮面板的设置一样。 Until that's done, the only workaround is to implement your own Dialog subclass. 在完成此操作之前,唯一的解决方法是实现自己的Dialog子类。

Turns out it's a bug (kinda). 原来这是一个错误(有点)。 You have to just implement your own Dialog to make it work right. 您只需要实现自己的对话框即可使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM