在Android上的AlertDialog内部,EditText扩展得太慢了

vtl*_*inh 9 android dialog android-animation android-alertdialog

简化更复杂的问题.

我有一个带有此视图dialog.xml的AlertDialog(我可能错了,这个问题与AlertDialog无关,但一般来说是Views):

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

我需要将整个对话框放在键盘正上方的底部,以便它与聊天程序的方式类似.我需要它作为浮动对话框,因为它不依赖于父视图中的任何一个位置.

我这样做(在客户端代码中)来实现:

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

现在,当我输入时,编辑字段正确地扩展到最多5行(从下往上).但是如果输入太快(按Enter键快速创建新线条),对话框展开动画(高度向下增加)比对话框向上移动动画要慢.当对话框看起来漂浮在空中时,这会导致1-2秒,其下边框和键盘之间有一点间隙.

我怎样才能防止这种情况(消除它们之间的这个空间)?或者让动画花费0秒?

澄清一下:我不需要为对话框弹出/关闭禁用动画.当窗口扩展太快时(例如通过创建新线条),窗口向上移动,而高度不能足够快地补偿移位,在对话框和键盘之间留下一个小的临时(1-2秒)间隙.这可能是浮动视图的基本问题(我尝试使用PopupWindow具有相同的效果).

对话与差距: 对话与差距

在1-2秒之后变成这个(当扩展动画最终赶上时): 对话无间隙

vtl*_*inh 5

经过大量挖掘堆栈溢出答案但没有结果后,我自己找到了解决方案。

这个想法是首先将整个对话框扩展到全屏,但有一个透明的覆盖视图覆盖空白部分。

dialog.xml 现在看起来像这样:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

  <!-- Transparent view that extends over entire screen and push comment view to the bottom -->
  <View
      android:id="@+id/overlay"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:background="@color/transparent"/>

  <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:background="@android:color/white"
      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>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

对于 Java 代码:

public void showDialog() {
  View content = activity.getLayoutInflater().inflate(R.layout.dialog, null, false);

  // This makes top LinearLayout background transparent, without this, 
  // it will be all white covering fullscreen.
  content.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));        

  Dialog dialog = new AlertDialog.Builder(context)
    .setView(content).create();
  dialog.show(); // needs to be done before window changes
  Window window dialog.getWindow();
  window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
  window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

  View overlay = content.findViewById(R.id.overlay);
  overlay.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
      if (dialog.isShowing()) {
        dialog.cancel();
      }
    }
  });
}
Run Code Online (Sandbox Code Playgroud)