按钮没有显示材料设计

3
我发现原因是我使用了Android-Iconics库——我去掉了上下文注入,现在一切正常。
我使用XML布局和Anko DSL的组合来构建我的应用程序,我注意到按钮设计取决于它是如何生成的。
如果是Anko生成的按钮,则文本为大写(我认为这应该是Material中的),并具有涟漪效果。如果按钮由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>

任何想法?谢谢!

你能分享一下你的XML吗? - Beyazid
@Beyazid 是的,抱歉 - 请查看更新后的问题。 - Naroh
@Naroh 你是在使用设计支持库还是新的材料设计库? - karan
@KaranMer 我现在正在使用新的 Material Design 库,但是设计支持库也发生了同样的事情。 - Naroh
2个回答

1

你的主题应该继承自 Theme.MaterialComponents.xxxxx

就像这个XML块一样

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">

你可以创建一个 TextView 类来设置为大写
class UppercaseTextView : TextView, ViewTreeObserver.OnPreDrawListener {

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}

    override fun setText(text: CharSequence, type: BufferType) {
        super.setText(text.toString().toUpperCase(), type)
    }
}

谢谢!我已经更改为扩展Theme.MaterialComponents.Light.NoActionBar,但我仍然得到一个不同的设计,其中文本是小写的。这在使用自定义样式或不设置样式时都会发生。截图:https://imgur.com/eGHzxUv - Naroh
我更新了我的帖子,但这个TextView类无法更改为设计模式。它只能用于将文本转换为大写。 - Beyazid

1
如果您正在使用新的Material Design组件,请确保应用程序主题扩展自主题 Theme.MaterialComponents 或其他相关主题。
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
    <!-- ... -->
</style>

此外,不要使用通用的 Button 类来定义按钮,您需要在xml和java中都使用 com.google.android.material.button.MaterialButton
<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"/>

嗨,Karan,谢谢!我尝试扩展Theme.MaterialComponents.Light.NoActionBar并使用视图com.google.android.material.button.MaterialButton,但与预览不同,应用程序仍然显示小写字母的按钮(我无法在XML中将其大写)。 - Naroh
在XML中设置android:textAllCaps="true"。完成更改后,请清除并构建您的项目。 - karan
在清理和重建项目之后,情况依旧(textAllCaps已经存在)。我真的不知道为什么会出现这种情况。 - Naroh
你正在从 Widget.AppCompat.Button 扩展你的按钮样式,你应该从 Widget.MaterialComponents.Button 进行扩展。 - karan
我发现问题的原因在于我使用了Android-Iconics库 - 我去掉了上下文注入,现在一切正常。 - Naroh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接