0

New to AS. Trying to use MaterialButton, overriding default background with @drawable, but it's not working.

Environment Android Studio: 4.0 Dependencies: implementation "androidx.appcompat:appcompat:$dependency_version_appcompat" implementation "com.google.android.material:material:$dependency_version_material"

Drawable

<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>

Layout

<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"
... />

With the above, I just get the default MaterialButton background.

If I set backgroundTint to @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"
... />

With the above, the drawable specs are recognized, but if I change the theme style, that doesn't work.

These are the only settings that work:

<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" />

Am I doing sth wrong...or is MaterialButton not quite there yet? I can live with the last solution, but it increases maintenance...

4

1 回答 1

0

使用以下方式定义您的应用主题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>

最后为您的按钮定义一个样式(对于深色和浅色模式,您不需要 2 种不同的样式):

<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文件夹中定义的颜色或属性。

于 2020-09-11T07:05:04.573 回答