具有自定义视图的AlertDialog不会"调整大小"

reV*_*rse 5 android android-layout android-alertdialog android-dialog

我遇到了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>
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的类/函数是在我的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();
        }
    });
Run Code Online (Sandbox Code Playgroud)

我尝试过以下的事情:

像这样将滚动条添加到LinearLayout

android:scrollbars="vertical" 
android:scrollbarAlwaysDrawVerticalTrack="true"
Run Code Online (Sandbox Code Playgroud)

结果:没有改变

为对话框设置标志:

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Run Code Online (Sandbox Code Playgroud)

结果:没有改变

将自定义样式应用于AlertDialog:

<style name="AlertDialog" parent="@android:style/Theme.Holo.Light">
        <item name="android:windowFullscreen">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)

Java的

builder = new AlertDialog.Builder(context, R.style.AlertDialog);
Run Code Online (Sandbox Code Playgroud)

这很有效.不幸的是,它产生了一些问题,您可以在下面的屏幕截图中看到

自定义样式 - 新问题

我很感激任何帮助,因为我几天来一直在努力解决这个问题.谢谢!

我现在使用以下方法在我的中创建对话框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();
    }
Run Code Online (Sandbox Code Playgroud)

nan*_*esh 7

我不知道你在哪里做了setSoftInputmode而不是

builder.show();
Run Code Online (Sandbox Code Playgroud)

使用

AlertDialog mDialog = builder.create();
mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mDialog.show();
Run Code Online (Sandbox Code Playgroud)

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