我正在尝试使用 MaterialComponents 在我的 Android 应用程序中为 AppBar 和 Toolbar 创建全局自定义样式。我希望这些风格能够在全球所有活动中应用。但是,定制并未得到体现。背景颜色和标题文字颜色不可见。
详情如下:
<resources>
<style name="GlobalButtonStyle" parent="Widget.AppCompat.Button">
<item name="textAllCaps">false</item>
<item name="android:backgroundTint">@color/dark_green</item>
<item name="android:textColor">@color/white</item>
</style>
<style name="CustomTextViewStyle" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/white</item>
</style>
<style name="CustomAppBarStyle" parent="Widget.MaterialComponents.AppBarLayout.Primary">
<item name="backgroundTint">#000080</item>
</style>
<style name="CustomTooolbarStyle" parent="Widget.MaterialComponents.Toolbar">
<item name="backgroundTint">#000080</item>
<item name="titleTextColor">@color/white</item>
</style>
</resources>
2. 主题中应用的样式 (themes.xml):
<resources>
<style name="Theme.EchoPoint" parent="Theme.AppCompat.Light.NoActionBar">
<item name="buttonStyle">@style/GlobalButtonStyle</item>
<item name="android:windowBackground">@color/black</item>
<item name="android:textViewStyle">@style/CustomTextViewStyle</item>
<item name="appBarLayoutStyle">@style/CustomAppBarStyle</item>
<item name="toolbarStyle">@style/CustomTooolbarStyle</item>
</style>
</resources>
3. activity_main.xml布局:
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/MainAppBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</com.google.android.material.appbar.AppBarLayout>
尽管在主题中应用了
appBarLayoutStyle
和 toolbarStyle
,背景仍然是白色,并且标题文本不可见。我有视力障碍,使用 Android TalkBack,我可以确认标题文本存在但不可见(可能是由于白色背景和白色文本)。
我尝试过的:
确认样式已应用到主题中。
确保父样式继承自MaterialComponents。
使用backgroundTint设置背景颜色。
预期结果:
#000080
,标题文本应为白色。问题:
其他背景:
TalkBack
进行可访问性测试。您使用了错误的属性来为工具栏的背景着色,即backgroundTint(它完成其工作,当您有任何背景时,如果您没有任何背景,则会着色)。你必须使用,
<item name="android:background">#000080</item>
,用于更改 AppBarLayout 和 MaterialToolbar 的背景颜色的属性。记住一件事,当你覆盖任何属性时,你必须选择正确的属性,否则结果会不同。在为特定小部件创建特定样式时(尤其是属性),请始终参考特定小部件的材质文档。
谈到第二个问题,如果您要在应用程序主题中为小部件指定自定义样式,例如`
<style name="Theme.Essentials" parent="Theme.AppCompat.Light.NoActionBar">
<item name="buttonStyle">@style/GlobalButtonStyle</item>
<item name="android:windowBackground">@color/black</item>
<item name="android:textViewStyle">@style/CustomTextViewStyle</item>
<item name="toolbarStyle">@style/CustomTooolbarStyle</item>
<item name="appBarLayoutStyle">@style/CustomAppBarStyle</item>"
</style>
it is applied to all the same widgets you create all in your activities , untill and unless you specifically set the style in widget's style attribute to a different one like
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/MainAppBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="Enter Your Style" />
`。 如果你改变了你的活动主题,那么你必须注意如何获得相同的风格。
更重要的是Material Design 3是目前最新的,但你正在使用Material Design 2。