更改菜单项颜色
Change menu items color
我正在尝试更改菜单项的颜色,但没有成功。
我想根据我在颜色文件中定义的 material 颜色系统更改菜单项的颜色。
这是我的 xml 文件。有什么想法吗?
app_bar_navigationdrawer.xml
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_navigation_drawer" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_baseline_add_circle_outline_24" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#ff0125</color> <!-- 6200EE -->
<color name="colorPrimaryVariant">#ff5a50</color> <!-- 3700B3 -->
<color name="colorSecondary">#ff9100</color>9e9e9e <!-- 03DAC6 -->
<color name="colorSecondaryVariant">#c56200</color> <!-- 018786 -->
<color name="colorBackground">#eeeeee</color> <!-- FFFFFF -->
<color name="colorSurface">#eeeeee</color> <!-- FFFFFF -->
<color name="colorError">#B00020</color>
<color name="colorOnPrimary">#FFFFFF</color>
<color name="colorOnSecondary">#000000</color>
<color name="colorOnBackground">#000000</color>
<color name="colorOnSurface">#000000</color>
<color name="colorOnError">#FFFFFF</color>
<color name="actionBarColor">#c30000</color>
</resources>
颜色-night.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#dc462b</color> <!-- BB86FC -->
<color name="colorPrimaryVariant">#ff6434</color> <!-- 3700B3 -->
<color name="colorSecondary">#8858c8</color> <!-- 03DAC6 -->
<color name="colorSecondaryVariant">#bb86fc</color> <!-- 03DAC6 -->
<color name="colorBackground">#121212</color>
<color name="colorSurface">#121212</color>
<color name="colorError">#CF6679</color>
<color name="colorOnPrimary">#000000</color>
<color name="colorOnSecondary">#000000</color>
<color name="colorOnBackground">#FFFFFF</color>
<color name="colorOnSurface">#FFFFFF</color>
<color name="colorOnError">#000000</color>
<color name="actionBarColor">#121212</color>
</resources>
正如您在附件中看到的,菜单项的颜色在深色主题下也不会因光线而改变。
您可以覆盖工具栏菜单中使用的颜色。
<com.google.android.material.appbar.MaterialToolbar
style="@style/Widget.MaterialComponents.Toolbar.Primary"
app:popupTheme="@style/AppTheme.PopupOverlay"
/>
与:
<style name="AppTheme.ToolabePopupOverlay" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<item name="colorSurface">@color/...</item> <!-- background -->
<item name="android:textColor">@color/...</item> <!-- text color -->
</style>
它也适用于 androidx.appcompat.widget.Toolbar
。
我正在尝试更改菜单项的颜色,但没有成功。
我想根据我在颜色文件中定义的 material 颜色系统更改菜单项的颜色。
这是我的 xml 文件。有什么想法吗?
app_bar_navigationdrawer.xml
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_navigation_drawer" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_baseline_add_circle_outline_24" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#ff0125</color> <!-- 6200EE -->
<color name="colorPrimaryVariant">#ff5a50</color> <!-- 3700B3 -->
<color name="colorSecondary">#ff9100</color>9e9e9e <!-- 03DAC6 -->
<color name="colorSecondaryVariant">#c56200</color> <!-- 018786 -->
<color name="colorBackground">#eeeeee</color> <!-- FFFFFF -->
<color name="colorSurface">#eeeeee</color> <!-- FFFFFF -->
<color name="colorError">#B00020</color>
<color name="colorOnPrimary">#FFFFFF</color>
<color name="colorOnSecondary">#000000</color>
<color name="colorOnBackground">#000000</color>
<color name="colorOnSurface">#000000</color>
<color name="colorOnError">#FFFFFF</color>
<color name="actionBarColor">#c30000</color>
</resources>
颜色-night.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#dc462b</color> <!-- BB86FC -->
<color name="colorPrimaryVariant">#ff6434</color> <!-- 3700B3 -->
<color name="colorSecondary">#8858c8</color> <!-- 03DAC6 -->
<color name="colorSecondaryVariant">#bb86fc</color> <!-- 03DAC6 -->
<color name="colorBackground">#121212</color>
<color name="colorSurface">#121212</color>
<color name="colorError">#CF6679</color>
<color name="colorOnPrimary">#000000</color>
<color name="colorOnSecondary">#000000</color>
<color name="colorOnBackground">#FFFFFF</color>
<color name="colorOnSurface">#FFFFFF</color>
<color name="colorOnError">#000000</color>
<color name="actionBarColor">#121212</color>
</resources>
正如您在附件中看到的,菜单项的颜色在深色主题下也不会因光线而改变。
您可以覆盖工具栏菜单中使用的颜色。
<com.google.android.material.appbar.MaterialToolbar
style="@style/Widget.MaterialComponents.Toolbar.Primary"
app:popupTheme="@style/AppTheme.PopupOverlay"
/>
与:
<style name="AppTheme.ToolabePopupOverlay" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<item name="colorSurface">@color/...</item> <!-- background -->
<item name="android:textColor">@color/...</item> <!-- text color -->
</style>
它也适用于 androidx.appcompat.widget.Toolbar
。