android - Android 上的 AlertDialog 内部 EditText 扩展太慢

标签 android dialog android-animation android-alertdialog

简化更复杂的问题。

我有一个带有此 View dialog.xml 的 AlertDialog(我可能错了,这个问题与 AlertDialog 无关,但与一般 View 无关):

<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>

我需要将整个对话框放在底部,就在键盘上方,这样它就类似于聊天程序的显示方式。我需要它作为 float 对话框,因为它没有绑定(bind)到父 View 中的任何一个地方。

我这样做(在客户端代码中)是为了实现这一点:

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 秒)间隙。这可能是一般 float View 的根本问题(我尝试使用具有相同效果的 PopupWindow)。

对话与差距: Dialog with the gap

1-2 秒后变成这样(当展开动画终于 catch 时): Dialog with no gap

最佳答案

在对堆栈溢出答案进行大量挖掘后没有结果后,我自己找到了解决方案。

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

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>

对于 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();
      }
    }
  });
}

关于android - Android 上的 AlertDialog 内部 EditText 扩展太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29907895/

相关文章:

android - 在 TextView 中从常规文本动画到粗体

android - 选定 ListView 项的向上滑动动画

java - Android 数字格式输出?

java - isGooglePlayServicesAvailable 返回 9

java - Android:为什么我的 onResume() DialogInterface 不断循环?

android - 滚动时如何扩展 RecyclerView Item 之间的空间

android - 如何将自定义 View 添加到 Horizo​​ntalScrollView?

android - 如何在 Android 中获取来电号码的运营商名称?

android - 是否有可能在单击 edittext 时显示对话框消息?

android - Spinner 的下拉菜单无法正常工作