使用 28.0.0-rc01 的按钮样式
Button styling with 28.0.0-rc01
我有以下按钮:
<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
:
但是 alpha2
、alpha3
和 rc01
看起来像这样:
为什么它会在更高版本(尤其是 RC)上忽略我的样式?也许我的 Button 定义存在根本性错误?
这是因为你的 AppTheme
是 Theme.MaterialComponents.Light.NoActionBar
,你的 activity 的充气器充气的是 MaterialButton
而不是 Button
并且您不能像更改旧 Button
的背景那样更改 MaterialButton
的背景(请参阅文档)。
如果您想要和以前一样的风格,请将您的 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>
请注意,在为视图编写样式(将在 style="@style/MyStyle
属性中使用)时,不应扩展 AppTheme
。
他们使用 alpha2 引入了 MaterialButton
的自动 inflation。
在 alpha1 和 alpha2 上打开 class android.support.design.theme.MaterialComponentsViewInflater
以查看差异。
如果您不希望这种自动充气行为 MaterialButton
将您的 AppTheme
更改为
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
我有以下按钮:
<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
:
但是 alpha2
、alpha3
和 rc01
看起来像这样:
为什么它会在更高版本(尤其是 RC)上忽略我的样式?也许我的 Button 定义存在根本性错误?
这是因为你的 AppTheme
是 Theme.MaterialComponents.Light.NoActionBar
,你的 activity 的充气器充气的是 MaterialButton
而不是 Button
并且您不能像更改旧 Button
的背景那样更改 MaterialButton
的背景(请参阅文档)。
如果您想要和以前一样的风格,请将您的 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>
请注意,在为视图编写样式(将在 style="@style/MyStyle
属性中使用)时,不应扩展 AppTheme
。
他们使用 alpha2 引入了 MaterialButton
的自动 inflation。
在 alpha1 和 alpha2 上打开 class android.support.design.theme.MaterialComponentsViewInflater
以查看差异。
如果您不希望这种自动充气行为 MaterialButton
将您的 AppTheme
更改为
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">