按钮样式与28.0.0-rc01

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

我有以下按钮:

 <Button                                    
     android:id="@+id/addexperience_button"                                    
     style="@style/MyButton"                                    
     android:layout_width="match_parent"                                    
     android:layout_height="wrap_content"                                    
     android:layout_alignLeft="@id/emptystate_image"                                    
     android:layout_alignRight="@id/emptystate_image"                                    
     android:layout_below="@id/emptystate_image"                                    
     android:text="@string/pickdetail_addexperience" />

这个造型:

  <style name="MyButton" parent="AppTheme">
        <item name="android:textColor">@color/white</item>
        <item name="android:paddingLeft">@dimen/single_spacing</item>
        <item name="android:paddingRight">@dimen/single_spacing</item>
        <item name="android:background">@drawable/bg_primarybutton</item>
    </style>

bg_primarybutton:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="6dp"
    android:insetLeft="4dp"
    android:insetRight="4dp"
    android:insetTop="6dp">
    <ripple android:color="?android:attr/colorControlHighlight">
        <item>
            <shape
                android:shape="rectangle"
                android:tint="@color/colorPrimary">
                <corners android:radius="18dp" />
            </shape>
        </item>
    </ripple>
</inset>

现在这个按钮与28.0.0-alpha1一样:

enter image description here

但与alpha2alpha3rc01看起来像这样:

enter image description here

为什么它在后来的版本中忽略了我的样式(特别是RC?)也许我的Button定义有根本性的错误?

android android-9.0-pie
1个回答
2
投票

原因是因为你的AppThemeTheme.MaterialComponents.Light.NoActionBar,你的活动的inflater膨胀MaterialButton而不是Button你不能改变MaterialButton的背景与旧的Button相同(见doc)。

如果你想拥有和以前一样的风格,请用你的MyButton样式替换

<style name="MyButton">
  <item name="android:textColor">@color/white</item>
  <item name="android:paddingLeft">@dimen/single_spacing</item>
  <item name="android:paddingRight">@dimen/single_spacing</item>
  <item name="backgroundTint">@color/colorPrimary</item>
  <item name="cornerRadius">18dp</item>
  <item name="rippleColor">?android:attr/colorControlHighlight</item>
</style>

请注意,在为View创建样式(将在AppTheme属性中使用)时,不应扩展style="@style/MyStyle

他们介绍了MaterialButton与alpha2的自动膨胀。在alpha1和alpha2上打开类android.support.design.theme.MaterialComponentsViewInflater以查看差异。

如果你不想要这种自动充气MaterialButton的行为改变你的AppThemeto

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge"> 
© www.soinside.com 2019 - 2024. All rights reserved.