Android覆盖预定义的父样式

问题描述 投票:0回答:1

我使用MaterialButton的预定义样式之一。

<com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Approve" />

这是我使用的预定义样式。

 </style>
    <style name="Widget.MaterialComponents.Button.OutlinedButton" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="android:paddingLeft">@dimen/mtrl_btn_padding_left</item>
    <item name="android:paddingRight">@dimen/mtrl_btn_padding_right</item>
    <item name="strokeColor">@color/mtrl_btn_stroke_color_selector</item>
    <item name="strokeWidth">@dimen/mtrl_btn_stroke_size</item>
  </style>

这是Widget.MaterialComponents.Button.OutlinedButton的父样式

 <style name="Widget.MaterialComponents.Button.TextButton" parent="Widget.MaterialComponents.Button.UnelevatedButton">
    <item name="android:textColor">@color/mtrl_text_btn_text_color_selector</item>
    <item name="android:paddingLeft">@dimen/mtrl_btn_text_btn_padding_left</item>
    <item name="android:paddingRight">@dimen/mtrl_btn_text_btn_padding_right</item>
    <item name="iconTint">@color/mtrl_text_btn_text_color_selector</item>
    <item name="iconPadding">@dimen/mtrl_btn_text_btn_icon_padding</item>
    <item name="backgroundTint">@color/mtrl_btn_text_btn_bg_color_selector</item>
    <item name="rippleColor">@color/mtrl_btn_text_btn_ripple_color</item>
  </style>

现在我尝试覆盖我的styles.xml中Widget.MaterialComponents.Button.TextButton的属性之一

 <style name="Widget.MaterialComponents.Button.TextButton">
        <item name="android:textColor">@color/colorAccent</item>
 </style>

我想知道这是否是覆盖预定义样式的正确方法,样式Widget.MaterialComponents.Button.OutlinedButton将使用@color/colorAccent作为文本颜色。我认为它可以正常工作,但是我不确定这种方法是否有副作用,或者有更好的方法吗?

android android-styles
1个回答
0
投票

您可以使用以下之一:

© www.soinside.com 2019 - 2024. All rights reserved.