我正在使用appcompat-v7 toolbar
并添加了一些带图标的菜单。
我的menu_items.xml
<item
android:id="@+id/quit"
android:title="Quit"
android:icon="@drawable/ic_power"
android:orderInCategory="700"
app:showAsAction="never"/>
<item
android:id="@+id/app_settings"
android:orderInCategory="600"
android:icon="@drawable/ic_cog"
app:showAsAction="never"
android:title="Settings"/>
<item
android:id="@+id/help"
android:orderInCategory="500"
android:title="Help"
android:icon="@drawable/ic_help"
app:showAsAction="always" />
<item
android:id="@+id/logout"
android:orderInCategory="400"
android:title="Logout"
android:icon="@drawable/ic_logout"
app:showAsAction="ifRoom" />
<item
android:id="@+id/tip"
android:orderInCategory="300"
android:title="Give Tip"
android:icon="@drawable/ic_coffee"
app:showAsAction="ifRoom" />
<item
android:id="@+id/withdraw"
android:orderInCategory="200"
android:title="Withdraw"
android:icon="@drawable/ic_bank"
app:showAsAction="ifRoom" />
<item
android:id="@+id/deposit"
android:orderInCategory="100"
android:title="Deposit"
android:icon="@drawable/ic_cash_multiple"
app:showAsAction="ifRoom" />
图标最初是黑色的,但如果我使用
,我希望它在工具栏上显示为白色app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
我的toolbar.xml
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="?attr/colorPrimary"/>
如何让图标显示为白色?
编辑:
答案 0 :(得分:5)
您可以像这样手动设置
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
for(int i = 0; i < menu.size(); i++){
Drawable drawable = menu.getItem(i).getIcon();
if(drawable != null) {
drawable.mutate();
drawable.setColorFilter(getResources().getColor(R.color.whiteColor), PorterDuff.Mode.SRC_ATOP);
}
}
return true;
}
特定图标:
MenuItem favoriteItem = menu.findItem(R.id.action_favorite);
Drawable newIcon = (Drawable)favoriteItem.getIcon();
newIcon.mutate().setColorFilter(Color.argb(255, 200, 200, 200), PorterDuff.Mode.SRC_IN);
favoriteItem.setIcon(newIcon);
如果您的方向更改运行时间,则可以使用条件检查方向并在此条件下设置菜单颜色。
像这样:@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// here you can set menu item color if it landScape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
或者如果您不想旋转屏幕,只需在清单文件中设置如下:
<activity android:name=".activities.MainActivity"
android:screenOrientation="portrait">
或者您可以将itemIconTint用于特定项目:
<item
android:id="@+id/quit"
android:title="Quit"
android:icon="@drawable/ic_power"
android:orderInCategory="700"
**app:itemIconTint="@color/black"**
app:showAsAction="never"/>
<item
android:id="@+id/app_settings"
android:orderInCategory="600"
android:icon="@drawable/ic_cog"
app:showAsAction="never"
**app:itemIconTint="@color/black"**
android:title="Settings"/>
答案 1 :(得分:0)
您的主题
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>
PopUp或菜单项主题如果你想要一些更像弹出窗口,大小和弹出菜单的背景你可以添加,如果你想要删除
<style name="PopupMenu" parent="android:Theme.Holo.Light">
<item name="android:popupBackground">@android:color/white</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">9sp</item>
<item name="textAppearanceLargePopupMenu">@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large</item>
<item name="textAppearanceSmallPopupMenu">@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small</item>
</style>
<强>颜色强>
<color name="white">#ffffffff</color>