简化一个更复杂的问题。
我有一个AlertDialog,它有一个名为dialog.xml的视图(我可能错了,这个问题可能与AlertDialog无关,但与一般的视图相关):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
// <ImageView 48dp x 48dp/> ImageView for profile photo
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:maxLines="5"
android:inputType="textMultiLine"
android:scrollbars="vertical"
android:scrollHorizontally="false"/>
// <ImageView 48dp x 48dp> ImageView for a send button.
</LinearLayout>
我希望整个对话框都位于底部,在键盘的正上方,这样就类似于聊天程序的界面。我需要它作为浮动对话框,因为它不会固定在父视图的任何一个位置。
为了实现这一点(在客户端代码中),我这样做:
dialog.show(); // above dialog
Window window dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
现在当我输入时,编辑框正确地展开到5行(从下往上)。但如果我输入得太快(按回车键快速创建新行),对话框的展开动画(高度向下增加)比对话框向上移动的动画慢。这导致在对话框出现时有1-2秒钟的时间它似乎漂浮在空中,其下边框和键盘之间存在一个小间隙。
我该如何防止这种情况发生(消除两者之间的空隙)?或让动画变为0秒?
澄清一下:我不需要禁用对话框弹出/关闭的动画。当窗口扩展得太快时(例如通过创建新行),窗口会向上移动,而高度没有足够快地补偿移动,留下了一个短暂的(1-2秒)间隙在对话框和键盘之间。这可能是浮动视图普遍面临的基本问题(我尝试使用PopupWindow也有同样的效果)。
带有间隙的对话框:
等展开动画最终赶上时1-2秒后,变成这样: