There's been many questions on styling on action bars, but the ones I've found either are relating to styling the tabs, or have answers that don't work for me.
关于动作条上的样式有很多问题,但我发现的问题要么是关于标签的样式,要么是对我不起作用的答案。
The question is really quite simple. I want to be able to change the text styling (even just colour) of the menu items in the action bar.
问题很简单。我希望能够更改操作栏中的菜单项的文本样式(甚至只是颜色)。
我读了这本书:http://android-developers.blogspot.com/2011/04/customizing-action-bar.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A + blogspot % 2 fhsdu + % 28 android +开发人员+博客% 29
And this question: Style an Action Bar in Android Honeycomb
这个问题是:在Android蜂巢中设置一个操作栏。
From which I have put together a test application that I am using to try and get the menu items to change. It uses all the default values for an app created in the eclipse android plugin, except for the following.
我在其中组装了一个测试应用程序,用于尝试更改菜单项。它使用eclipse android插件中创建的应用程序的所有默认值,以下是例外。
A styles file:
一个样式文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:titleTextStyle">@style/MyActionBar.TitleTextStyle</item>
<item name="android:actionMenuTextAppearance">@style/MyActionBar.MenuTextStyle</item>
</style>
<style name="MyActionBar.TitleTextStyle"
parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#F0F</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">24dip</item>
</style>
<style name="MyActionBar.MenuTextStyle"
parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#F0F</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">24dip</item>
</style>
</resources>
A menu for the action bar:
操作栏的菜单:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:showAsAction="always|withText" android:icon="@android:drawable/ic_menu_edit"
android:id="@+id/menu_item1" android:title="menu_item1"></item>
<item android:showAsAction="always|withText" android:icon="@android:drawable/ic_menu_edit"
android:id="@+id/menu_item2" android:title="menu_item2"></item>
</menu>
The main activity:
主要活动:
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/**
* Create the options menu that is shown on the action bar
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
}
The application compiles and runs. The styling for the action bar title text works perfectly (is that lovely shade of pink #F0F I've defined). The menu items do not change appear but with default styling (holo light).
应用程序编译并运行。动作条标题文本的样式效果非常好(就是我定义的粉红色#F0F的可爱阴影)。菜单项不改变显示,但使用默认样式(全息灯)。
What am I doing wrong ?
我做错了什么?
7 个解决方案
#1
56
Instead of having the android:actionMenuTextAppearance
item under your action bar style, move it under your app theme.
不要在你的动作条样式下使用android:actionMenuTextAppearance项目,而要在你的应用主题下移动它。
#2
5
I know its an old post, but just in case anyone is looking here. I added this to my style.xml and it worked for me.
我知道这是一个老帖子,但以防有人在看。我把这个加入我的风格中。xml对我很有用。
<!-- This is the main theme parent -->
<style name="MyTabStyle">
<item name="android:actionBarTabTextStyle">@style/MyTabTextStyle</item>
</style>
<style name="MyTabTextStyle" parent="@android:style/Widget.ActionBar.TabText">
<item name="android:textAppearance">@android:style/TextAppearance.Medium</item>
<item name="android:textSize">14sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/pressed_skylogtheme</item>
</style>
#3
4
You have to change
你必须改变
<style name="MyActionBar.MenuTextStyle"
parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
to
来
<style name="MyActionBar.MenuTextStyle"
parent="android:style/TextAppearance.Holo.Widget.ActionBar.Menu">
as well. This works for me.
。这适合我。
#4
4
I think the code below
我想下面的代码。
<item name="android:actionMenuTextAppearance">@style/MyActionBar.MenuTextStyle</item>
must be in MyAppTheme
section.
必须在appmyapptheme部分。
#5
2
Chris answer is working for me...
克里斯的回答是为我工作…
My values-v11/styles.xml file:
我的values-v11 /风格。xml文件:
<resources>
<style name="LightThemeSelector" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/ActionBar</item>
<item name="android:editTextBackground">@drawable/edit_text_holo_light</item>
<item name="android:actionMenuTextAppearance">@style/MyActionBar.MenuTextStyle</item>
</style>
<!--sets the point size to the menu item(s) in the upper right of action bar-->
<style name="MyActionBar.MenuTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textSize">25sp</item>
</style>
<!-- sets the background of the actionbar to a PNG file in my drawable folder.
displayOptions unset allow me to NOT SHOW the application icon and application name in the upper left of the action bar-->
<style name="ActionBar" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:background">@drawable/actionbar_background</item>
<item name="android:displayOptions"></item>
</style>
<style name="inputfield" parent="android:Theme.Holo.Light">
<item name="android:textColor">@color/red2</item>
</style>
</resources>
#6
2
I did this way:
我做了这种方式:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionMenuTextAppearance">@style/MenuTextAppearance</item>
<item name="android:actionMenuTextAppearance">@style/MenuTextAppearance</item>
<item name="actionMenuTextColor">@color/colorAccent</item>
</style>
<style name="MenuTextAppearance" >
<item name="android:textAppearance">@android:style/TextAppearance.Large</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
</style>
#7
1
My guess is that you have to also style the views that are generated from the menu information in your onCreateOptionsMenu(). The styling you applied so far is working, but I doubt that the menu items, when rendered with text use a style that is the same as the title part of the ActionBar.
我的猜测是,您还必须对从onCreateOptionsMenu()中的菜单信息生成的视图进行样式化。到目前为止,您所应用的样式是有效的,但是我怀疑用文本呈现的菜单项是否使用了与ActionBar的标题部分相同的样式。
You may want to look at Menu.getActionView() to get the view for the menu action and then adjust it accordingly.
您可能需要查看menu . getactionview()以获取菜单操作的视图,然后相应地调整它。
#1
56
Instead of having the android:actionMenuTextAppearance
item under your action bar style, move it under your app theme.
不要在你的动作条样式下使用android:actionMenuTextAppearance项目,而要在你的应用主题下移动它。
#2
5
I know its an old post, but just in case anyone is looking here. I added this to my style.xml and it worked for me.
我知道这是一个老帖子,但以防有人在看。我把这个加入我的风格中。xml对我很有用。
<!-- This is the main theme parent -->
<style name="MyTabStyle">
<item name="android:actionBarTabTextStyle">@style/MyTabTextStyle</item>
</style>
<style name="MyTabTextStyle" parent="@android:style/Widget.ActionBar.TabText">
<item name="android:textAppearance">@android:style/TextAppearance.Medium</item>
<item name="android:textSize">14sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/pressed_skylogtheme</item>
</style>
#3
4
You have to change
你必须改变
<style name="MyActionBar.MenuTextStyle"
parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
to
来
<style name="MyActionBar.MenuTextStyle"
parent="android:style/TextAppearance.Holo.Widget.ActionBar.Menu">
as well. This works for me.
。这适合我。
#4
4
I think the code below
我想下面的代码。
<item name="android:actionMenuTextAppearance">@style/MyActionBar.MenuTextStyle</item>
must be in MyAppTheme
section.
必须在appmyapptheme部分。
#5
2
Chris answer is working for me...
克里斯的回答是为我工作…
My values-v11/styles.xml file:
我的values-v11 /风格。xml文件:
<resources>
<style name="LightThemeSelector" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/ActionBar</item>
<item name="android:editTextBackground">@drawable/edit_text_holo_light</item>
<item name="android:actionMenuTextAppearance">@style/MyActionBar.MenuTextStyle</item>
</style>
<!--sets the point size to the menu item(s) in the upper right of action bar-->
<style name="MyActionBar.MenuTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textSize">25sp</item>
</style>
<!-- sets the background of the actionbar to a PNG file in my drawable folder.
displayOptions unset allow me to NOT SHOW the application icon and application name in the upper left of the action bar-->
<style name="ActionBar" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:background">@drawable/actionbar_background</item>
<item name="android:displayOptions"></item>
</style>
<style name="inputfield" parent="android:Theme.Holo.Light">
<item name="android:textColor">@color/red2</item>
</style>
</resources>
#6
2
I did this way:
我做了这种方式:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionMenuTextAppearance">@style/MenuTextAppearance</item>
<item name="android:actionMenuTextAppearance">@style/MenuTextAppearance</item>
<item name="actionMenuTextColor">@color/colorAccent</item>
</style>
<style name="MenuTextAppearance" >
<item name="android:textAppearance">@android:style/TextAppearance.Large</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
</style>
#7
1
My guess is that you have to also style the views that are generated from the menu information in your onCreateOptionsMenu(). The styling you applied so far is working, but I doubt that the menu items, when rendered with text use a style that is the same as the title part of the ActionBar.
我的猜测是,您还必须对从onCreateOptionsMenu()中的菜单信息生成的视图进行样式化。到目前为止,您所应用的样式是有效的,但是我怀疑用文本呈现的菜单项是否使用了与ActionBar的标题部分相同的样式。
You may want to look at Menu.getActionView() to get the view for the menu action and then adjust it accordingly.
您可能需要查看menu . getactionview()以获取菜单操作的视图,然后相应地调整它。