Android开发显示头部Bar
需求:
显示如下图:
显示头部Bar,颜色也能自定义。
解决方案
这个修改是在如下三个文件里进行修改:
按顺序修改:
themes.xml(night):
<resources xmlns:tools="http://schemas.android.com/tools"><!-- Base application theme. --><style name="Base.Theme.CropIrrigation" parent="Theme.MaterialComponents.Light.DarkActionBar"><!-- Customize your dark theme here. --><item name="colorPrimary">@color/my_dark_primary</item></style>
</resources>
themes.xml:
<resources xmlns:tools="http://schemas.android.com/tools"><!-- Base application theme. --><style name="Base.Theme.CropIrrigation" parent="Theme.MaterialComponents.Light.DarkActionBar"><!-- Customize your light theme here. --><!-- <item name="colorPrimary">@color/my_light_primary</item> --><!-- Customize your light theme here. --><item name="colorPrimary">@color/myBarColor</item>.<item name="titleTextColor">@color/myBarColor</item></style><style name="Theme.CropIrrigation" parent="Base.Theme.CropIrrigation" />
</resources>
colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources><color name="black">#FF000000</color><color name="white">#FFFFFFFF</color><color name="myBarColor">#673AB7</color><color name="my_dark_primary">#673AB7</color><color name="titleColor">#E3D9D9</color><color name="colorPrimary">#03A9F4</color>
</resources>
在主题文件**themes.xml:中,起作用的就是 Theme.MaterialComponents.Light.DarkActionBar
,在属性文件colors.xml:**中起作用的就是 <color name="colorPrimary">#03A9F4</color>
,<color name="my_dark_primary">#673AB7</color>