这是与主题 http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems 相关的开发指南
所以我们有 Activity
public class MyActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.first_menu_button:
return true;
case R.id.second_menu_button:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
R.menu.my_menu 在哪里
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/first_menu_button"
android:showAsAction="always"
android:icon="@drawable/btn_first"/>
<item
android:id="@+id/second_menu_button"
android:showAsAction="always"
android:icon="@drawable/btn_second"/>
</menu>
为按钮设置样式 http://developer.android.com/guide/topics/ui/actionbar.html#ActionItemStyles假设我们应该使用 android:actionButtonStyle 属性。我是这样做的:
在 list 中:
<activity android:name="com.root.test.MyActivity"
android:theme="@style/CustomActionBarStyle"
</activity>
在 styles.xml 中:
<style name="CustomActionBarStyle" parent="@android:style/Theme.Holo.Light">
<item name="android:actionButtonStyle">@style/customActionButtonStyle</item>
<item name="android:windowActionBar">true</item>
<item name="android:windowNoTitle">false</item>
</style>
<style name="customActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">
<item name="android:[...]
</style>
问题是无论我在 customActionButtonStyle
中写什么,它都会忽略它。 CustomActionBarStyle
中的其他属性有效(甚至更复杂,为了简单起见,它们只是被省略了)。我的主要目的是设置自定义填充。有没有其他方法可以做到这一点,就像其他方法一样,而不是 android:actionButtonStyle
属性?或者有人知道如何完成这项工作? (android:abItemPadding 属性仅在 3.1 中添加)
谢谢。
最佳答案
如果您想更改文本参数,如文本大小、颜色或样式,您必须使用 actionMenuTextAppearance。这是我的 ActionBarSherlock 代码:
<item name="actionMenuTextAppearance">@style/MyProject.ActionMenuTextAppearance</item>
<item name="android:actionMenuTextAppearance">@style/MyProject.ActionMenuTextAppearance</item>
...
<style name="MyProject.ActionMenuTextAppearance" parent="TextAppearance.Sherlock.Widget.ActionBar.Menu">
<item name="android:textSize">15sp</item>
<item name="android:textStyle">normal</item>
<item name="android:textAllCaps">false</item>
</style>
编辑:这是我的模板 styling action bar使用 ActionBarSherlock:https://github.com/petrnohejl/Android-Templates-And-Utilities/tree/master/Res-Theme-Holo-Deprecated
关于安卓 3.0 : cannot style Action Bar 's action item buttons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8254476/