最近,我们移动到我们的应用程序使用材料设计主题(我们使用的是1.4.0版本)。作为其中的一部分,我们将显式的<com.google.android.material.button.MaterialButton声明转换为<Button,因为Button将在幕后转换为MaterialButton。除了下面这种情况之外,其他一切都运行得很好。
我们在FrameLayout中有一个<com.google.android.material.button.MaterialButton,如下所示
<FrameLayout 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:layout_height="match_parent"
android:layout_width="match_parent">
<Button
...
...
/>
</FrameLayout>并通过视图绑定将其用作自定义视图,如下所示
class MyView: FrameLayout {
private val binding by ViewGroupBindingProperty(MyViewBinding::inflate)
constructor(...)
/// other code
}当我尝试将上面的
Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to com.google.android.material.button.MaterialButton
at jp.ne.paypay.android.app.view.paymentMethod.MyView.init(MyView.kt:51)
at xx.xx.xx.xx.MyView.<init>(MyView.kt:29)
任何关于哪里可能出错的指导都是非常感谢的。提前谢谢你。
发布于 2021-10-18 08:40:33
我认为您需要在xml中将按钮指定为com.google.android.material.button.MaterialButton,而不是普通的Button,尽管在使用material样式时,这应该由布局膨胀器来完成
发布于 2021-10-19 02:23:35
@braj请给我你的完整代码,然后你会问为什么
<按钮声明在我的情况下不起作用
因为Button是Android SDK的一部分,所以你可以使用Material按钮材料设计的一部分,它们是相似的,但Material按钮有很多优点,你可以用Material Design编写漂亮的代码
在您的情况下,必须将视图实现为Material Button,而不仅仅是button
https://stackoverflow.com/questions/69612237
复制相似问题