我的菜单项变为红色,10sp,背景为白色,但textStyle粗体不起作用.为什么是这样?
styles.xml
<style name="toolbarMenuTheme"> <item name="android:colorBackground">@color/white</item> <item name="android:textColor">@color/red</item> <item name="android:textSize">10sp</item> <item name="android:textStyle">bold</item> </style>
我的布局片段.
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="48dp" android:layout_marginTop="-5dp" android:background="@color/white" android:theme="@style/toolbarMenuTheme"/>
我的膨胀布局.
<item android:id="@+id/web_view_reload" android:icon="@drawable/replay" android:title="Reload" app:showAsAction="always"/> <item android:id="@+id/web_view_action" android:icon="@drawable/stack_icon_on" android:title="Stack" app:showAsAction="always"/> <item android:id="@+id/web_view_screenshot" android:icon="@drawable/screenshot" android:title="Screenshot" app:showAsAction="always"/> <item android:id="@+id/web_view_share" android:title="SHARE WEBSITE"/> <item android:id="@+id/web_view_copy_url" android:title="Copy URL"/>
用java填充它:
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.drawable.x); toolbar.inflateMenu(R.menu.web_view_toolbar_menu);
并设置单击侦听器:
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { .... } }
评论后编辑
这是我的完整styles.xml
Styles.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomUITheme" parent="@style/Theme.AppCompat.Light.NoActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowActionBar">false</item> <item name="windowActionBar">false</item> <item name="android:windowBackground">@color/milky</item> <!--item name="android:textSelectHandle">@drawable/text_select_handle_middle</item> <item name="android:textSelectHandleLeft">@drawable/text_select_handle_left</item> <item name="android:textSelectHandleRight">@drawable/text_select_handle_right</item--> <item name="colorPrimary">@color/toolbar_bg</item> <item name="colorPrimaryDark">#ff404040</item> <item name="colorAccent">@color/accent</item> <item name="android:textColorHighlight">@color/text_highlight</item> <!--item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item--> </style> <style name="toolbarMenuTheme" parent="TextAppearance.Widget.AppCompat.Toolbar.Title"> <item name="android:colorBackground">@color/white</item> <item name="android:textColor">@color/red</item> <item name="android:textSize">10sp</item> <item name="android:textStyle">bold</item> </style> <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> <item name="android:background">#FFFFFF</item> <item name="android:windowNoTitle">true</item> </style> <!--style name="CustomActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse"> <item name="android:background">@color/toolbar_bg</item> </style--> <style name="captionOnly"> <item name="android:background">@null</item> <item name="android:clickable">false</item> <item name="android:focusable">false</item> <item name="android:minHeight">0dp</item> <item name="android:minWidth">0dp</item> </style> <!-- FAB --> <style name="FloatingActionButton" parent="android:Widget.ImageButton"> <item name="floatingActionButtonSize">normal</item> </style> <!-- empty states --> <style name="empty_title"> <item name="android:textSize">10dp</item> </style> <style name="empty_details"> <item name="android:textColor">#999999</item> <item name="android:textSize">8dp</item> </style> </resources>
解决方法
在styles.xml文件中添加以下内容
<style name="ActionBar.nameText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title"> <item name="android:textColor">@color/PrimaryTextColor</item> <item name="android:textSize">18sp</item> <item name="android:textStyle">bold</item> </style>
样式名称和父级可能在您的情况下有所不同,但将概念设为粗体
编辑
在toolbar.xml中使用以下内容
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:companyApp="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="?actionBarSize" companyApp:theme="@style/ActionBarThemeOverlay" companyApp:titleTextAppearance="@style/ActionBar.nameText"> </android.support.v7.widget.Toolbar>
在你的styles.xml中
<style name="ActionBarThemeOverlay" parent=""> <item name="android:textColorPrimary">@color/PrimaryTextColor</item> <item name="colorControlHighlight">@color/BackgroundColor</item> <item name="android:actionMenuTextColor">@color/PrimaryTextColor</item> <item name="android:textColorSecondary">@color/PrimaryTextColor</item> <item name="android:background">@color/PrimaryBackgroundColor</item> </style>
并在androidmanifest.xml中
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Your Theme">