【发布时间】: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