Android Material Button-无法识别@drawable规格

问题描述

AS的新手。尝试使用MaterialButton,使用@drawable覆盖默认背景,但不起作用。

环境 Android Studio:4.0 依存关系: 实现“ androidx.appcompat:appcompat:$ dependency_version_appcompat” 实现“ com.google.android.material:material:$ dependency_version_material”

可绘制

<shape xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:shape="rectangle">
    <solid
            app:backgroundTint ="?attr/ColorBackgroundAppBar">
    </solid>
    <stroke
            android:width="1px"
            android:color="?attr/ColorBorderDark">
    </stroke>
</shape>

布局

<com.google.android.material.button.MaterialButton
android:id="@+id/common_appbar_btn_lang_mode"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background = "@drawable/common_appbar_btn_background"
android:elevation="@dimen/common_elevation_height_btn"
... />

有了上面的内容,我得到了默认的MaterialButton背景。

如果我将backgroundTint设置为@null

<com.google.android.material.button.MaterialButton
android:id="@+id/common_appbar_btn_lang_mode"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
app:backgroundTint="@null"
android:background = "@drawable/common_appbar_btn_background"
android:elevation="@dimen/common_elevation_height_btn"
... />

使用上述方法,可以识别可绘制的规格,但是如果我更改主题样式,则无法使用。

这些是唯一有效的设置:

<com.google.android.material.button.MaterialButton
android:id="@+id/common_appbar_btn_lang_mode"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:backgroundTint = "?attr/ColorBackgroundAppBar"
app:strokeColor="?attr/ColorBackgroundAppBar"
app:strokeWidth="0px"
android:elevation="@dimen/common_elevation_height_none"
android:fontFamily="@font/font_family_kaiti"
android:text="简"
android:textAlignment="center"
android:textColor="?attr/ColorTextPrimary"
android:textStyle="normal"
android:textFontWeight="700"
android:textSize="24dp" />

我做错什么了吗?还是MaterialButton还不存在?我可以接受最后一个解决方案,但是它增加了维护...

解决方法

使用Theme.MaterialComponents.DayNight定义您的应用主题:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryVariant">@color/colorPrimaryLight</item>
    <item name="colorSecondary">@color/colorSecondary</item>
    <!-- ....  -->
</style>

然后,如果需要,定义明暗模式的颜色:

示例:res\values\colors.xml

   <color name="colorPrimary">.....</color>

res\values-night\colors.xml文件夹中定义相同的颜色:

   <color name="colorPrimary">.....</color>

最后为您的按钮定义一种样式(暗和亮模式不需要两种不同的样式):

<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@color/....</item>
    <item name="strokeColor">@color/....</item>
    <item name="strokeWidth">@dimen/....</item>
</style>

最后一步,将样式分配给布局中的按钮。

<com.google.android.material.button.MaterialButton
   style="@style/Widget.App.Button"
   .../>

或在您的应用主题中全局:

<item name="materialButtonStyle">@style/Widget.App.Button</item>

对于您的Button,由于您未使用渐变,因此可以使用MaterialButton提供的标准属性。

如果您希望将android:background属性与自定义可绘制对象一起使用,则可以使用:

<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@null</item>
    <item name="android:background">@drawable/.....</item>
</style>

然后在可绘制对象内部仅使用在应用程序主题以及res\values\colors.xmlres\values-night\colors.xml文件夹中定义的颜色或属性。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...