当设置视图时,Android警示对话框出现空白空间

4
我有一个问题。我制作了一个带有“不再显示”勾选框的警报对话框,其中显示了一条消息和视图。当我在安卓4.4上运行此应用程序时,它显示正常。但是当我在安卓4.1上运行时,就像这张图片所示,它会显示一个空白的位置(当视图只包含高度为0dp的线性布局时,我也会得到这个空白的位置):http://foto.modelbouwforum.nl/images/2014/08/15/Screenshot2014-08-15-14-33-40.png 我的警报对话框代码:
final SharedPreferences sharedPreferences=getSharedPreferences("appPrefs", Context.MODE_PRIVATE);
        if(!sharedPreferences.getBoolean("isTutorialMainScreenChecked", false)){
            View checkBoxView = View.inflate(MainScreenHandler.this, R.layout.checkbox_alert_dialog,null);
            checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox);
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setMessage(getString(R.string.alertDialog_main_screen_tutorial));
            builder.setCancelable(false);
            builder.setView(checkBoxView);
            builder.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if(checkBox.isChecked()){
                                SharedPreferences.Editor editor=sharedPreferences.edit();
                                editor.putBoolean("isTutorialMainScreenChecked", true);
                                editor.commit();
                            }
                            dialogInterface.dismiss();
                        }
                    });
            builder.setNegativeButton(getString(R.string.alertDialog_cancel),
                    new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialogInterface, int i){
                            dialogInterface.cancel();
                        }
                    });
            alertDialog = builder.create();
            alertDialog.show();

这是我的复选框布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:orientation="vertical"
             android:baselineAligned="false">
    <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#ffff3bb2"/>

    <CheckBox
            style="@android:style/TextAppearance.Small"
            android:textColor="#ff000000"
            android:text="@string/alertDialog_checkbox_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/checkbox"/>
</LinearLayout>

谢谢您的提前帮助。

PS:

以下是在 Android KK 上的显示效果:http://foto.modelbouwforum.nl/images/2014/08/15/QuickMemo2014-08-15-14-42-41.png

3个回答

3

我查看了API 19中的 alert_dialog.xml 布局源代码,并发现以下布局代码可用于插入自定义视图:

<FrameLayout android:id="@+id/customPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <FrameLayout android:id="@+android:id/custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dip"
        android:paddingBottom="5dip" />
</FrameLayout>

然后我尝试设置背景色以查看哪个视图占据了空间。 customPanel 以红色显示,自定义视图以绿色显示。

AlertDialog d = builder.create();
d.show();

View v = (View)d.getWindow().findViewById(android.R.id.custom).getParent();
v.setBackgroundColor(Color.RED);

这张图片表示customPanel应该有一些填充,或者custom应该有非零的布局参数或其他。 在检查那些等于零的属性之后,我尝试测试customPanel的minimumHeight,在xxhdpi或64dp上得到192。 这个设置导致框架比嵌套的自定义视图要大。

我还没有找出这个设置是在哪里应用的,但这里有一个解决方法:

AlertDialog d = builder.create();
d.show();

View v = (View)d.getWindow().findViewById(android.R.id.custom).getParent();
v.setMinimumHeight(0);

在API 18、19上进行了测试。


1
运行得非常好!奇怪他们给那个布局指定了最小高度! - LimpSquid
我已经为这个问题苦苦思索了一个小时,这个技巧很有效,谢谢! - JasonWyatt

2
除了vokilam的回答之外:
如果您正在使用android.support.v7.app.AlertDialog。
在API 27上测试过(几乎可以确定这也适用于API 28)
布局:
<FrameLayout
    android:id="@+id/contentPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="48dp">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Space
                android:id="@+id/textSpacerNoTitle"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dialog_padding_top_material" />

            <TextView
                android:id="@+id/message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingEnd="?attr/dialogPreferredPadding"
                android:paddingStart="?attr/dialogPreferredPadding"
                style="@style/TextAppearance.Material.Subhead" />

            <Space
                android:id="@+id/textSpacerNoButtons"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dialog_padding_top_material" />
        </LinearLayout>
    </ScrollView>
</FrameLayout>

<FrameLayout
    android:id="@+id/customPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="48dp">

    <FrameLayout
        android:id="@+id/custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</FrameLayout>

对于contentPanel和customPanel,android:minHeight="48dp" 导致了不必要的空白间隔。

不必要的空白间隔对话框

我所做的是:

最初的回答:

alertDialog.SetView(view);
alertDialog.show();
...
View customPanel = (View) view.getParent().getParent();
if (customPanel != null)
    customPanel.setMinimumHeight(0);
View contentPanel = (View) alertDialog.findViewById(android.R.id.message).getParent().getParent().getParent();
if (contentPanel != null)
    contentPanel.setMinimumHeight(contentPanel.getMinimumHeight() * 2 / 3);

Result:

Blank space removed


0

你尝试过调整填充和边距吗? 你可以复制你的布局到res/layout-vXX中,并设置一个特定的(负)边距。


无论我是否设置负边距,都没有关系,因为空白空间不是视图的一部分。正如我所说,当视图高度为0dip时,同样高的空白空间也存在。 - LimpSquid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接