【发布时间】:2021-12-05 06:46:57
【问题描述】:
最近,我们开始为我们的应用使用材料设计主题(我们使用的是 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)
【问题讨论】:
-
希望this链接对你有帮助
-
@Rustam 谢谢,但我不希望以编程方式创建按钮。我已经有了 XML 中的按钮。它在膨胀和使用它时抛出异常
标签: android material-design android-button material-components-android