点击按钮如何动态调整MotionLayout布局的height与width参数
悬浮窗动态调整宽高失效问题解决方案
问题现象
现有硬编码宽高的布局,添加加减号按钮点击动态调整布局宽高时功能不生效。
相关代码
XML布局代码
<androidx.constraintlayout.motion.widget.MotionLayout android:id="@+id/floating_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/black" app:layoutDescription="@xml/floating_play_layout_scene"> <VideoView android:id="@+id/floating_video" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/close_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:alpha="0" android:src="@drawable/float_close" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/float_playbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:alpha="0" android:src="@drawable/ic_play" app:layout_constraintBottom_toBottomOf="@id/floating_video" app:layout_constraintEnd_toEndOf="@id/floating_video" app:layout_constraintStart_toStartOf="@id/floating_video" app:layout_constraintTop_toTopOf="@id/floating_video" /> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/fullscreen_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:alpha="0" android:src="@drawable/full_screen" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.appcompat.widget.AppCompatImageButton android:id="@+id/add_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/_10sdp" android:layout_marginBottom="@dimen/_10sdp" android:background="@color/transparent" android:src="@drawable/ic_add_enabled" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <androidx.appcompat.widget.AppCompatImageButton android:id="@+id/min_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/_5sdp" android:layout_marginBottom="@dimen/_10sdp" android:background="@color/transparent" android:src="@drawable/ic__remove_enabled" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@id/add_btn" /> </androidx.constraintlayout.motion.widget.MotionLayout>
原业务逻辑代码
addbtn.setOnClickListener { height.plus(10) width.plus(10) params = WindowManager.LayoutParams( width, height, LAYOUT_FLAG, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT ) }
WindowManager初始化逻辑
windowManager.addView(mFloatingView, params)
失效原因
- Kotlin中
Int.plus()方法仅返回相加后的新值,不会修改原变量本身,原写法中height.plus(10)和width.plus(10)执行后,height和width变量的实际值没有发生任何变化。 - 即使正确更新了params的宽高值,仅修改params对象不会同步到已添加的悬浮窗,需要主动调用WindowManager的
updateViewLayout方法通知系统更新悬浮窗参数。
修正后代码
// 初始化阶段先定义初始宽高、初始化LayoutParams并添加悬浮窗 private var currentWidth = 200 // 替换为你的初始宽度值 private var currentHeight = 300 // 替换为你的初始高度值 private lateinit var windowParams: WindowManager.LayoutParams // 初始化悬浮窗时执行 windowParams = WindowManager.LayoutParams( currentWidth, currentHeight, LAYOUT_FLAG, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT ) windowManager.addView(mFloatingView, windowParams) // 加号按钮点击逻辑 add_btn.setOnClickListener { currentWidth += 10 currentHeight += 10 // 可选:添加上限判断,避免超出屏幕显示范围 windowParams.width = currentWidth windowParams.height = currentHeight windowManager.updateViewLayout(mFloatingView, windowParams) } // 减号按钮点击逻辑 min_btn.setOnClickListener { // 增加下限判断,避免宽高过小无法交互 if (currentWidth > 100 && currentHeight > 100) { currentWidth -= 10 currentHeight -= 10 windowParams.width = currentWidth windowParams.height = currentHeight windowManager.updateViewLayout(mFloatingView, windowParams) } }
内容的提问来源于stack exchange,提问作者Muhammad Sajid
相关产品推荐
相关产品推荐

