gpt4 book ai didi

android - 以编程方式删除 FloatingActionButton 边框?

转载 作者:行者123 更新时间:2023-11-29 23:30:32 25 4
gpt4 key购买 nike

FloatingActionButton 预先配置了 0.5dp 边框,这违反了 Google 自己的 Material 指南。

您可以通过 app:borderWidth = 0dp 移除 XML 中的边框

有没有办法以编程方式更改此属性?

最佳答案

没有一种干净的方法可以做到这一点。

您可以通过反射来完成,使用以下代码 (Kotlin):

val button: FloatingActionButton     // your button, initialized someway

// set the field mBorderWidth of your button, it's not enough to do this because this value has already been consumed for drawing the button's background; it shouldn't be necessary, anyway set it...
val fieldBorderWidth = FloatingActionButton::class.java.getDeclaredField("mBorderWidth")
fieldBorderWidth.isAccessible = true
fieldBorderWidth.setInt(button, 0)

// the trick is to call again setBackgroundDrawable (it's already been called inside of the constructor) on the field mImpl of the FloatingActionButton; you can call it with the button's parameters but passing 0 as borderWidth. So obtain the needed button's parameters and call that method
// (since all these fields and methods are private you must set everything as accessible)

// get button's parameters
val fieldBackgroundTint = FloatingActionButton::class.java.getDeclaredField("mBackgroundTint")
fieldBackgroundTint.isAccessible = true
val fieldBackgroundTintValue = fieldBackgroundTint.get(button)
val fieldBackgroundTintMode = FloatingActionButton::class.java.getDeclaredField("mBackgroundTintMode")
fieldBackgroundTintMode.isAccessible = true
val fieldBackgroundTintModeValue = fieldBackgroundTintMode.get(button)
val fieldRippleColor = FloatingActionButton::class.java.getDeclaredField("mRippleColor")
fieldRippleColor.isAccessible = true
val fieldRippleColorValue = fieldRippleColor.get(button)

// get button's mImpl field
val methodGetImpl = FloatingActionButton::class.java.getDeclaredMethod("getImpl")
methodGetImpl.isAccessible = true
val fieldImplValue = methodGetImpl.invoke(button)

// get mImpl's setBackgroundDrawable method and call it
val methodSetBackgroundDrawable = fieldImplValue.javaClass.getDeclaredMethod("setBackgroundDrawable", ColorStateList::class.java, PorterDuff.Mode::class.java, Int::class.java, Int::class.java)
methodSetBackgroundDrawable.isAccessible = true
methodSetBackgroundDrawable.invoke(fieldImplValue, fieldBackgroundTintValue, fieldBackgroundTintModeValue, fieldRippleColorValue, 0)

关于android - 以编程方式删除 FloatingActionButton 边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52769887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com