Android SoftInputMode为SOFT_INPUT_ADJUST_RESIZE(adjustResize)时没有resize动画的问题

本文深入分析了在使用android:windowSoftInputMode=adjustResize时,编辑框位置变化出现alpha动画的现象,并揭示了当窗口背景设置为null时,这种动画效果消失的原因,帮助开发者理解并解决相关问题。

用过android:windowSoftInputMode=”adjustResize”的应该都知道,在该模式下弹出软键盘时,编辑框变位置时会有一个alpha动画。昨天突然发现一个界面里在弹出软键盘时没有了这个alpha动画,resize就显得很生硬。于是就想找下原因,网上也找不到类似的情况,看了好多源码,终于找到原因如下:
getWindow().setBackgroundDrawable(null);
就是这一行导致的,在PhoneWindow里setBackgroundDrawable()函数会调用updateWindowResizeState():

        void updateWindowResizeState() {
            Drawable bg = getBackground();
            hackTurnOffWindowResizeAnim(bg == null || bg.getOpacity()
                    != PixelFormat.OPAQUE);
        }

也就是如果window的backgroundDrawable为null或者不透明时,windowResize时就不会有动画效果。

package com.weishitechsub.kdcxqwb.dialog; import android.app.Dialog; import android.content.Context; import android.graphics.Rect; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.bottomsheet.BottomSheetBehavior; import com.google.android.material.bottomsheet.BottomSheetDialog; import com.google.android.material.bottomsheet.BottomSheetDialogFragment; import com.weishitechsub.kdcxqwb.R; import com.weishitechsub.kdcxqwb.bean.ListBean; import com.weishitechsub.kdcxqwb.fragment.Adapter.CourierAdapter; import java.util.List; public class BottomSheetDialogUtil { public interface OnCourierSelectedListener { void onCourierSelected(String selectedCourier, String num); } public static void showBottomSelectionDialog(Context context, List<ListBean> courierList, OnCourierSelectedListener listener) { BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context, R.style.BottomSheetStyle); View view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_selection, null); EditText etSearch = view.findViewById(R.id.et_search); RecyclerView recyclerView = view.findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(context)); // 创建适配器 CourierAdapter courierAdapter = new CourierAdapter(courierList, (courier, num) -> { bottomSheetDialog.dismiss(); if (listener != null) { listener.onCourierSelected(courier, num); } }); recyclerView.setAdapter(courierAdapter); // 🔍 添加搜索功能 etSearch.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { courierAdapter.filter(s.toString()); } @Override public void afterTextChanged(Editable s) {} }); // 设置 BottomSheet 行为 View bottomSheetInternal = view.findViewById(R.id.design_bottom_sheet); // 如果你的 layout 根布局有这个 id if (bottomSheetInternal == null) { // 否则用 content view bottomSheetInternal = view; } BottomSheetBehavior<View> behavior = BottomSheetBehavior.from(bottomSheetInternal); behavior.setDraggable(true); behavior.setHalfExpandedRatio(0.6f); behavior.setState(BottomSheetBehavior.STATE_EXPANDED); // ✅ 关键:让整个 BottomSheet 可以随键盘上推 // 注意:必须在 setCancelable 之前调用,否则无效 bottomSheetDialog.setContentView(view, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT )); // 获取 Window 并设置 softInputMode Window window = bottomSheetDialog.getWindow(); if (window != null) { window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); window.setSoftInputMode(android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); } // ✅ 可选:打开键盘 etSearch.post(() -> { etSearch.requestFocus(); InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(etSearch, InputMethodManager.SHOW_IMPLICIT); }); bottomSheetDialog.show(); } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="400dp" android:orientation="vertical" android:id="@+id/design_bottom_sheet" android:background="@android:color/white"> <!-- 标题栏 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="?attr/colorPrimary"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选择运输商" android:textSize="18sp" android:textStyle="bold" android:gravity="center" android:padding="16dp" android:textColor="@android:color/white" /> </LinearLayout> <!-- 分隔线 --> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray" /> <!-- 搜索框 --> <EditText android:id="@+id/et_search" android:layout_width="match_parent" android:layout_height="48dp" android:layout_margin="12dp" android:padding="12dp" android:hint="搜索快递公司..." android:textSize="14sp" android:background="@drawable/shuru_bg" android:drawableStart="@drawable/ic_search_sized" android:drawablePadding="8dp" /> <!-- 使用RecyclerView替代ListView,避免滑动冲突 --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="vertical" /> </LinearLayout>报错 java.lang.IllegalArgumentException: The view is not a child of CoordinatorLayout
最新发布
11-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值