【问题标题】:AlertDialog with custom view doesn't "resize"具有自定义视图的 AlertDialog 不会“调整大小”
【发布时间】:2013-02-21 00:49:51
【问题描述】:

我在使用 AlertDialog 和自定义内容/视图时遇到问题。简单地说,当软键盘打开时,AlertDialog 不会自行调整大小。以下屏幕截图最能说明我的问题和我想要实现的目标:

当前行为(左)和想要的行为(右)

我知道在 SO 上还有一些其他线程存在类似问题。不幸的是,提供的解决方案都不适合我。按照我的示例代码:

XML

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

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

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </EditText>
    </LinearLayout>

</ScrollView>

Java - CustomAlertDialog.class

public class CustomAlertDialog extends AlertDialog{

    private Context context;

    public CustomAlertDialog(Context context) {
        super(context);
        this.context = context;
    }

    public void buildDialog(){
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_content, null);
        builder = new AlertDialog.Builder(context);
        builder.setTitle("EditText");
        builder.setView(layout);
        builder.setPositiveButton("Ok", new OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                dismiss();
            }
        });
        builder.create();
    }

    public void showDialog(){
        builder.show();
    }
}

上面的类/函数在我的 Main.java 类中按下按钮时调用

btnAlertDialog.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            CustomAlertDialog dialog = new CustomAlertDialog(Main.this);
            dialog.buildDialog();
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            dialog.showDialog();
        }
    });

我尝试了以下方法:

像这样向 LinearLayout 添加滚动条

android:scrollbars="vertical" 
android:scrollbarAlwaysDrawVerticalTrack="true"

结果:没有任何改变

为对话框设置标志:

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

结果:没有任何改变

对 AlertDialog 应用自定义样式:

<style name="AlertDialog" parent="@android:style/Theme.Holo.Light">
        <item name="android:windowFullscreen">false</item>
</style>

Java

builder = new AlertDialog.Builder(context, R.style.AlertDialog);

这行得通。不幸的是,它产生了一些问题,您可以在以下屏幕截图中看到这些问题

感谢任何帮助,因为我已经为这个问题苦苦挣扎了好几天。谢谢!

解决方案

我现在使用以下方法在我的CustomAlertDialog.class 中创建对话框。请参阅我在nandeesh 的答案下方的评论,以了解为什么它以前不起作用的更多详细信息。

public void startDialog(){
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_content, null);
        builder = new AlertDialog.Builder(context);
        builder.setTitle("EditText");
        builder.setView(layout);
        builder.setPositiveButton("Ok", new OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                dismiss();
            }
        });
        AlertDialog aDialog = builder.create();
        aDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        aDialog.show();
    }

【问题讨论】:

  • 使用布局权重。不匹配父项
  • match_parent 非常适合宽度。此外,不可能为单个 LinearLayout 赋予权重。
  • 好吧,基本上按钮不会显示,因为您正在对对话框使用自定义视图。在你给它特定的主题之后,按钮就会显示出来。如果我为对话框创建了自定义视图,我也会使用自定义按钮。
  • 魔法不是主题本身,而是。使用自定义按钮会导致同样的问题 + 我必须为不同的 api 级别应用特定的样式。 AlertDialog 自己处理特定于 api 的样式。这就是为什么我想使用 AlerDialog 而不是自定义的东西。

标签: android android-layout android-alertdialog android-dialog


【解决方案1】:

我不确定你在哪里设置了 setSoftInputmode 但不是

builder.show();

使用

AlertDialog mDialog = builder.create();
mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mDialog.show();

如果这不起作用,请发布 CustomAlertDialog 类

【讨论】:

  • 太棒了,现在可以了。我已经用我的CustomAlertDialog.class 的完整代码编辑了我的问题。似乎问题在于我从未明确创建过 AlertDialog 。因此,当我在 Main.class 中设置标志时,它没有任何效果,因为显示的对话框不是我的 Main.class 的对话框,而是构建器的对话框。现在我在 CustomAlertDialog 类中设置标志并显式创建一个 AlertDialog 并且它可以工作。再次感谢,您让我找到了解决方案。
【解决方案2】:

显然这个问题与Dialog的构建方式无关,如果你使用自定义View等,问题是windowSoftInputMode。

在 AndroidManifest.xml 中为 Activity 更改 windowSoftInputMode 并为 Window 更改它应该可以解决问题。

确保对话框中的视图是 ScrollView。

【讨论】:

  • android:windowSoftInputMode="adjustResize" 应用于我的活动并不能解决问题,我之前也尝试过,根据参考手册,这只会影响主窗口。此外:正如您在我的 xml 代码中看到的那样,有一个滚动视图,如果行数超过 >whole 的行数,它可以正常工作
猜你喜欢
  • 2013-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多