我遇到了 AlertDialog 和自定义内容/ View 的问题。简单地说,当打开软键盘时,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();
}
最佳答案
我不确定你在哪里设置了 setSoftInputmode 但不是
builder.show();
使用
AlertDialog mDialog = builder.create();
mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mDialog.show();
如果这不起作用,请发布 CustomAlertDialog 类
关于android - 带有自定义 View 的 AlertDialog 不是 "resize",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14921339/