材质按钮中定义的样式不包含AppTheme

时间:2019-09-11 05:03:26

标签: android material-design android-button material-components-android material-components

  

由于我在所有项目中使用的“材质”按钮丢失了主题颜色,因此我根据新的材质指南更改了“ Apptheme”属性。

我尝试通过将默认值设置为其父级并使其更改其主要颜色和辅助颜色来制作自定义样式。

Button的XML文件

<com.google.android.material.button.MaterialButton
    android:id="@+id/bs_create_business"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left|center_vertical"
    android:text="@string/create_business"
    android:textAllCaps="true"
    app:backgroundTint="@color/browser_actions_bg_grey" />

styles.xml

    <style name="AppTheme"parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="colorPrimary">@color/foopprimary_500</item>
        <item name="colorPrimaryDark">@color/foopprimary_900</item>
        <item name="colorAccent">@color/foopsecondary_500</item>
        <itemname="android:windowBackground">@android:color/white</item>
        <item name="fontFamily">@string/default_font</item>

        <item name="colorSecondary">@color/foopsecondary_500</item>
        <item name="colorOnSecondary">@color/foopprimary_500</item>
    </style>

我的输出是这样的: enter image description here

1 个答案:

答案 0 :(得分:3)

要更改Widget.MaterialComponents.Button.OutlinedButton样式的颜色,只需使用lke:

  <style name="MyButton" parent="Widget.MaterialComponents.Button.OutlinedButton" >
    <!-- Border color -->
    <item name="strokeColor">@color/stroke_color_selector</item>
    <!-- Text color -->
    <item name="android:textColor">@color/text_color_selector</item>
    <!-- Background color -->
    <item name="backgroundTint">@color/text_btn_bg_color_selector</item>
  </style>

enter image description here

选择器的默认值为:

StrokeColor:

  <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/colorPrimary" android:state_checked="true"/>
    <item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_checked="false"/>
  </selector>

TextColor:

  <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_checkable="true" android:state_checked="true" android:state_enabled="true"/>
    <item android:alpha="0.60" android:color="?attr/colorOnSurface" android:state_checkable="true" android:state_checked="false" android:state_enabled="true"/>
    <item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_enabled="true"/>
    <item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
  </selector>

背景:

  <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="0.08" android:color="?attr/colorPrimary" android:state_checked="true"/>
    <item android:color="@android:color/transparent" android:state_checked="false"/>
  </selector>