我发现原因是我正在使用 Android-Iconics 库 - 我删除了上下文注入(inject),现在一切正常。
我正在结合使用 XML 布局和 Anko DSL 来构建我的应用程序,我注意到按钮设计因生成方式而异。
如果它是 Anko 生成的按钮,文本是大写的(我认为它应该在 Material 中)并且有链式 react 。如果按钮是由 XML 创建的,则文本为小写且没有效果。
上面的按钮是 XML 按钮,因此您可以在这里看到区别。
我试过为按钮设置自定义样式,但它似乎不起作用 - 我什至无法使 textAllCaps=true 起作用。
目前我正在使用 androidx 并扩展 AppCompat & Theme.AppCompat.Light.NoActionBar 并且我尝试扩展 Widget.AppCompat.Button 以设置自定义样式到没有运气的观点。
这发生在所有 API 级别(24、26 和 28)中。在 XML 预览中它确实显示正常。
当前的XML是
<Button
android:text="@string/compartir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/debunkShareButton"
android:textAllCaps="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toBottomOf="@+id/debunkTitle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
style="@style/Full_Yellow_Button"/>
Full_Yellow_Button 是
<style name="Full_Yellow_Button" parent="Widget.AppCompat.Button">
<item name="android:background">@drawable/yellow_gradient</item>
</style>
有什么想法吗?谢谢!
最佳答案
如果您使用新的 Material 设计组件,请确保您的应用程序主题扩展自主要主题 Theme.MaterialComponents
或其他相关主题。
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
<!-- ... -->
</style>
此外,您需要在 xml 和 java 中使用 com.google.android.material.button.MaterialButton
而不是使用通用 Button
类来定义按钮.
<com.google.android.material.button.MaterialButton
android:id="@+id/material_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_label_enabled"/>
关于android - 按钮不显示 Material Design,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55494139/