Android:在 Android 2.3 中,对话框不是全屏或没有奇怪背景的 AlertDialog

标签 android dialog android-alertdialog

我正在开发一个 Android 应用程序,其中有带有自定义 layoutDialogs。起初我决定使用没有标题和按钮的 AlertDialog,因为我在自定义 layout 中有自己的 Buttons。这在 Android 4.4 中工作正常,但在 Android 2.3 中不正常。

这是它在 Android 4.4 中的显示方式:

enter image description here

这是它在 Android 2.3 中的显示方式:

enter image description here

请注意我的 Dialog 后面奇怪的灰色背景。

在 SO 中我看到有些人使用 Dialog 而不是 AlertDialog 解决了它。我试过了,它确实修复了它,但现在我的 Dialog 填满了整个屏幕:

enter image description here

我不想这样,我希望 DialogAlertDialog 一样有一定的边距。

这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_background"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/adTitleTextView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginTop="1dp"
        android:background="@drawable/background_green"
        android:gravity="center"
        android:textColor="#FFFFFFFF"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#FFFFFFFF" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:paddingBottom="15dp"
        android:paddingTop="10dp" 
        android:textColor="#FFFFFFFF" />

    <LinearLayout
        android:id="@+id/adButtonBar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/adNegativeButton"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/selector_button_alert_dialog"
            android:textColor="#FFFFFFFF"
            android:textSize="14sp" />

        <Button
            android:id="@+id/adNeutralButton"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/selector_button_alert_dialog"
            android:textColor="#FFFFFFFF"
            android:textSize="14sp" />

        <Button
            android:id="@+id/adPositiveButton"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/selector_button_alert_dialog"
            android:textColor="#FFFFFFFF"
            android:textSize="14sp" />

    </LinearLayout>

</LinearLayout>

这就是我创建 Dialog 的方式:

    Dialog mDialog = new Dialog( this, android.R.style.Theme_Dialog );
    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    View content = getLayoutInflater().inflate( R.layout.alert_dialog_layout, null);
    ((TextView)content.findViewById(R.id.adTitleTextView)).setText( title );
    ((TextView) content.findViewById( R.id.adBodyTextView )).setText( message );    

    mDialog.setContentView( content );

    Button b = (Button) content.findViewById(R.id.adPositiveButton);
    b.setText( posText );
    b.setOnClickListener( this );

    b = (Button) content.findViewById(R.id.adNegaveButton);
    b.setText( negText );
    b.setOnClickListener( this );

    mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    mDialog.show();

这就是我创建 AlertDialog 的方式:

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( ctx );

    alertDialogBuilder.setView( content );

    final AlertDialog mDialog = alertDialogBuilder.create();
    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    ...

任何人都知道如何从 Android 2.3 中的 AlertDialog 中删除那个奇怪的背景,或者如何使 Dialog 不填满整个屏幕?

谢谢!

最佳答案

虽然我不太喜欢,但我终于找到了解决方案。我正在解决 Dialog 不是全屏问题。

基本上,我以编程方式设置宽度,如果高度超过某个值,我也设置它:

    content.getLayoutParams().width = (int) (screenWidth * 0.95);

    content.post(new Runnable() {

        @Override
        public void run() {             

            int newMaxHeight = content.findViewById(R.id.adTitleTextView).getHeight() + 
                    content.findViewById(R.id.adBodyContainer).getHeight() + 
                    content.findViewById(R.id.adButtonBar).getHeight();             


            if( newMaxHeight > screenHeight * 0.92 ){

                content.getLayoutParams().height = (int) (screenHeight * 0.92);

                content.findViewById(R.id.adBodyContainer).getLayoutParams().height = 
                        (int) (screenHeight * 0.92) - content.findViewById(R.id.adTitleTextView).getHeight()
                        - content.findViewById(R.id.adButtonBar).getHeight();

            }

        }
    });

通过这段代码,我可以限制对话框的最大宽度和高度。如果有人找到更好的解决方案,请在此处发布。

关于Android:在 Android 2.3 中,对话框不是全屏或没有奇怪背景的 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22091347/

相关文章:

android - 如何使 Android CheckedTextView 中的复选框左对齐而不是右对齐?

java - 从 ASP 转换的 JSON 日期 - 需要转换为 Java

javascript - jQuery UI - 对话框将自定义效果选项传递到对话框以进行显示和隐藏

java - 是否可以动态生成看起来不错的属性对话框?

android - 警报对话框中的按钮样式

android - 当键盘按下时从对话框中的 editText 中移除焦点

Android Alert Dialog - 如何在按下 OK 按钮后隐藏它

android - 如何在 Fresco Library 中实现缩放?

java - LinearLayout 从右到左填充

java - 关闭 JDialog Java