android.app.ActionBar $LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams

本文介绍了如何解决在Android开发中遇到的LayoutParams类型转换错误问题,提供了具体的修改方法,包括删除错误的LayoutParams导入路径并替换正确的ViewGroup.LayoutParams。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误提示:

android.app.ActionBar $LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams



修改方法:

将原来import的LayoutParams库android.app.ActionBar $LayoutParams删除,将android.widget.FrameLayout imoort进来即可。



===================================================================================

错误提示:

android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams


修改方法:

LayoutParams topBarPara= (LayoutParams)topBar.getLayoutParams();

改为:

ViewGroup.LayoutParams topBarPara= (ViewGroup.LayoutParams)topBar.getLayoutParams();

package com.ailuxi.luxitrans; import android.app.Activity; import android.graphics.Rect; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private EditText textInputEditText; private Button recordButton; private boolean isTextMode = true; // 默认文字模式 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN ); setContentView(R.layout.activity_main); SoftHideKeyBoardUtil(this); } // // 在onCreate()中调用 // public static void assistActivity(Activity activity) { // SoftHideKeyBoardUtil(activity); // } private View mChildOfContent; private int usableHeightPrevious; private void SoftHideKeyBoardUtil(Activity activity) { FrameLayout content = activity.findViewById(android.R.id.content); mChildOfContent = content.getChildAt(0); mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent); } private void possiblyResizeChildOfContent() { int usableHeightNow = computeUsableHeight(); if (usableHeightNow != usableHeightPrevious) { int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); int heightDifference = usableHeightSansKeyboard - usableHeightNow; if (heightDifference > (usableHeightSansKeyboard / 4)) { // 键盘弹出时调整布局高度 mChildOfContent.getLayoutParams().height = usableHeightSansKeyboard - heightDifference; } else { // 键盘收起时恢复高度 mChildOfContent.getLayoutParams().height = usableHeightSansKeyboard; } mChildOfContent.requestLayout(); usableHeightPrevious = usableHeightNow; } } private int computeUsableHeight() { Rect rect = new Rect(); mChildOfContent.getWindowVisibleDisplayFrame(rect); return rect.bottom - rect.top; } } 确实生效了但是键盘弹出时布局跟键盘之间有空隙如何适配
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值