android:windowsoftinputmode=“adjustresize” 无效的解决办法

本文针对Android应用中出现的布局问题提供了几种解决方案,包括调整布局类型、取消全屏模式及设置fitsSystemWindows等。

1.可能是布局有问题(适当的使用relativelayout,尽量少使用LinearLayout)

程序之前的大致结构如下:

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <LinearLayout>  
  4.   
  5.         <ViewFlipper>  
  6.             <include />  
  7.         </ViewFlipper>  
  8.   
  9.         <Button  
  10.             android:layout_width="match_parent"  
  11.             android:layout_height="45dp"  
  12.             android:layout_alignParentBottom="true"  
  13.             android:layout_gravity="center_horizontal"  
  14.             android:layout_marginBottom="50dp"  
  15.             android:layout_marginLeft="20dp"  
  16.             android:layout_marginRight="20dp"  
  17.             android:background="@drawable/btn_regist_next"  
  18.             android:gravity="center"  
  19.             android:textColor="@color/white" />  
  20.     </LinearLayout>  
  21. </RelativeLayout>  

解决办法:
将上述的LinearLayout改为RelativeLayout即可


2.window是全屏模式,请取消设置全屏模式

这个目前没有很好地额解决方式


3.将activity的根布局设置 fitsSystemWindows="true".

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、付费专栏及课程。

余额充值