Android悬浮窗显隐mainMenu时固定mainButton位置的问题
问题原因
你当前的布局采用垂直方向的LinearLayout作为外层容器,当mainMenu设置为GONE时,它会完全脱离布局流、不再占用空间。如果你的悬浮窗WindowManager参数设置为高度自适应(比如wrap_content),整个悬浮窗的高度会随内容变化,进而导致mainButton的位置跟随窗口整体偏移。
解决方案
通过使用RelativeLayout或ConstraintLayout替换外层LinearLayout,将mainButton所在的topMenu固定在悬浮窗顶部,mainMenu放在其下方,这样无论mainMenu显隐,mainButton的位置都不会改变。
方案1:使用RelativeLayout修改布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/topMenu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true"> <ImageButton android:id="@+id/mainButton" android:layout_width="60dp" android:layout_height="60dp" android:src="@mipmap/robot_foreground" /> </LinearLayout> <LinearLayout android:id="@+id/mainMenu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/topMenu" android:orientation="vertical" tools:visibility="visible"> <Button android:id="@+id/menu1" android:layout_width="wrap_content" android:layout_height="60dp" android:drawableLeft="@drawable/ic_launcher_foreground" android:text="Item1" /> <Button android:layout_width="wrap_content" android:layout_height="60dp" android:drawableLeft="@drawable/baseline_6_ft_apart_24" android:text="Item2" /> </LinearLayout> </RelativeLayout>
layout_alignParentTop="true"强制topMenu固定在父布局顶部layout_below="@id/topMenu"让mainMenu始终位于topMenu下方,其显隐不会影响topMenu的位置
方案2:使用ConstraintLayout修改布局
如果项目中已经引入了ConstraintLayout,也可以用以下布局:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/topMenu" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent"> <ImageButton android:id="@+id/mainButton" android:layout_width="60dp" android:layout_height="60dp" android:src="@mipmap/robot_foreground" /> </LinearLayout> <LinearLayout android:id="@+id/mainMenu" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintTop_toBottomOf="@id/topMenu" tools:visibility="visible"> <Button android:id="@+id/menu1" android:layout_width="wrap_content" android:layout_height="60dp" android:drawableLeft="@drawable/ic_launcher_foreground" android:text="Item1" /> <Button android:layout_width="wrap_content" android:layout_height="60dp" android:drawableLeft="@drawable/baseline_6_ft_apart_24" android:text="Item2" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
app:layout_constraintTop_toTopOf="parent"将topMenu与父布局顶部绑定app:layout_constraintTop_toBottomOf="@id/topMenu"让mainMenu固定在topMenu下方
额外说明
保持原有的显隐逻辑不变即可,无需修改Java/Kotlin代码:
if (mainMenu.getVisibility() == View.VISIBLE) { mainMenu.setVisibility(View.GONE); } else { mainMenu.setVisibility(View.VISIBLE); }
内容的提问来源于stack exchange,提问作者Nader Naderi
相关产品推荐
相关产品推荐

