android - 按钮不显示 Material Design

标签 android android-layout anko

我发现原因是我正在使用 Android-Iconics 库 - 我删除了上下文注入(inject),现在一切正常。

我正在结合使用 XML 布局和 Anko DSL 来构建我的应用程序,我注意到按钮设计因生成方式而异。

如果它是 Anko 生成的按钮,文本是大写的(我认为它应该在 Material 中)并且有链式 react 。如果按钮是由 XML 创建的,则文本为小写且没有效果。

Screenshot

上面的按钮是 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/

相关文章:

java - Android 中 Dropbox 中的内部服务器错误

android - 在按钮上,文本和可绘制对象在居中时距离太远

android - Activity 生命周期观察者 - 如何知道 Activity 实例何时被销毁?

Android:具有 match_parent 的 LinearLayout 与父级不匹配

android - 使用 AppCompat 时如何更改 ActionBar 标题字体

android - 类型不匹配。必需的 map <String, Any> 找到的 map <String, Any?>

android - 在 Corona 小部件中显示在模拟器中但不在设备上

java - 显示 WebService URL 连接超时 Android 的警报消息

android - 从 Kotlin Anko 中的两个表中选择数据

android - Anko协程扩展的正确使用方法是什么?