Android材质按钮-无法识别@可绘制规格



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>

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

示例:resvaluescolors.xml:

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

resvalues-nightcolors.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>

然后在drawable中只使用应用程序主题以及resvaluescolors.xmlresvalues-nightcolors.xml文件夹中定义的颜色或属性。

最新更新