Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可以实时设置 layoutChangedGravity 吗? #252

Open
dyguests opened this issue Dec 3, 2024 · 0 comments
Open

可以实时设置 layoutChangedGravity 吗? #252

dyguests opened this issue Dec 3, 2024 · 0 comments

Comments

@dyguests
Copy link

dyguests commented Dec 3, 2024

我的悬浮布局大概长这样:

F-----

它是可以被收起的,收起后长这样:

F

问题在于,我的吸附方式是水平吸附:setSidePattern(SidePattern.RESULT_HORIZONTAL)

这里在吸附到左侧时,悬浮而已收起时正常:

|-----------------------|
|F-----                 |

->

|-----------------------|
|F                      |

但是在右侧时,会变成这样:

|-----------------------|
|                 F-----|

->

|-----------------------|
|                F      |

-(动画)->

|-----------------------|
|                      F|

所以我的想法是可以动态修改layoutChangedGravity
但是我目前只能用反射:

    private fun setLayoutChangedGravity(gravity: Int) {
        // FloatingWindowManager.getHelper(TAG_SECURITY)?.config?.layoutChangedGravity = gravity
        try {
            val floatingWindowManagerClass = Class.forName("com.lzf.easyfloat.core.FloatingWindowManager")
            val instanceField = floatingWindowManagerClass.getDeclaredField("INSTANCE")
            instanceField.isAccessible = true
            val floatingWindowManagerInstance = instanceField.get(null)
            val getHelperMethod = floatingWindowManagerClass.getDeclaredMethod("getHelper", String::class.java)
            getHelperMethod.isAccessible = true
            val floatingWindowHelper = getHelperMethod.invoke(floatingWindowManagerInstance, TAG_SECURITY)
            val floatingWindowHelperClass = floatingWindowHelper.javaClass
            val configField = floatingWindowHelperClass.getDeclaredField("config")
            configField.isAccessible = true
            val config = configField.get(floatingWindowHelper)
            val configClass = config.javaClass
            val layoutChangedGravityField = configClass.getDeclaredField("layoutChangedGravity")
            layoutChangedGravityField.isAccessible = true
            layoutChangedGravityField.set(config, gravity)
        } catch (e: Exception) {
            Lg.e("反射修改 LayoutChangedGravity 失败", e)
        }

有什么更好的办法吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant