【发布时间】:2020-08-15 23:03:16
【问题描述】:
目前我正在使用com.google.android.material:material:1.1.0 库。
我创建了 Base Theme 并添加了 Material Button 全局样式,例如:<item name="materialButtonStyle">@style/Material.BaseAppTheme.Button</item>。它适用于所有默认按钮。现在的问题是我想要 Material Text Button 和 Material Outlined Button 的特定样式。但是即使我在属性中添加特定样式,默认样式也会覆盖特定样式。
喜欢这个:
<Button
style="@style/Widget.MaterialComponents.Button.TextButton"
...>
仍然显示为默认按钮。所以我必须删除 Base Theme 中的全局按钮样式,并在每个 中添加样式。我不知道这是为不同类型的材质按钮应用样式的唯一方法。或者我错过了一些东西。 这是我的主题和样式代码
基础主题
BaseTheme.Button 和 TextAppearance 样式
<style name="TextAppearance.Base.Button" parent="TextAppearance.MaterialComponents.Button">
<item name="android:fontFamily">@string/font_family_regular</item>
<item name="fontFamily">@string/font_family_regular</item>
<item name="android:textColor">?buttonTextColorPrimary</item>
<item name="android:textStyle">normal</item>
<item name="android:textSize">@dimen/_10sp</item>
<item name="android:textAllCaps">false</item>
</style>
<style name="Material.BaseAppTheme.Button" parent="Widget.MaterialComponents.Button">
<item name="android:paddingLeft">@dimen/_8dp</item>
<item name="android:paddingRight">@dimen/_8dp</item>
<item name="android:background">?buttonColorEnabled</item>
</style>
XML 中的按钮
<Button
style="@style/Widget.MaterialComponents.Button.TextButton"
android:id="@+id/tv_footer_cta_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textAllCaps="true"
android:text="@{viewModel.footerLeftTitle}"
app:invisibility="@{viewModel.footerLeftCTAVisibility}"
app:onSingleClick="@{() -> viewModel.navigateTo(viewModel.footerLeftCTAUri)}"
tools:text="MANAGE ACCOUNT" />
<Button
android:id="@+id/tv_footer_cta_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="@{viewModel.footerRightTitle}"
android:textAllCaps="true"
app:invisibility="@{viewModel.footerRightCTAVisibility}"
app:onSingleClick="@{() -> viewModel.navigateTo(viewModel.footerRightCTAUri)}"
tools:text="MANAGE ACCOUNT" />
当前结果
** 预期结果**
【问题讨论】:
-
我还尝试了 Material.IO material.io/develop/android/components/buttons/#theming-buttons 推荐的主题。通过定义不同的主题覆盖解决了不覆盖但现在在未应用主题覆盖中设置 textApppearence 的问题。
-
您的代码似乎是正确的(但使用
backgroundTint而不是android:background)。你在用tv_footer_cta_left.setChecked(true)的东西吗? -
@GabrieleMariotti 我已经尝试过风格和按钮的背景色,但没有成功。不,我没有使用 .setChecked(true/false)
标签: android android-layout material-design android-jetpack material-components-android