Android 3.0: cannot style Action Bar 's action item buttons

為{幸葍}努か 提交于 2020-01-01 19:33:30

问题


Here is the dev guide related to the subject http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems

So we have in 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);
    }
}

Where R.menu.my_menu is

<?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>

To style that buttons http://developer.android.com/guide/topics/ui/actionbar.html#ActionItemStyles say we should use android:actionButtonStyle attribute. I've done it like this:

In manifest:

<activity android:name="com.root.test.MyActivity"
    android:theme="@style/CustomActionBarStyle"
</activity>

In 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>

Problem is that whatever I write in customActionButtonStyle, it's just ignoring it. Other atributes in CustomActionBarStyle works (even more complicated, they are jast omit for the sake of simplicity). My main purpuse was to set custom padding. Is there any other way to do that, like some other, not android:actionButtonStyle attribute? Or some one knows how to get this work? (android:abItemPadding attribute added only in 3.1)

Thanks.


回答1:


Most style elements will work but padding is supplied by the system layouts used to generate the action buttons and likely will not do what you expect. Most action bar metrics such as padding and margins should generally be left alone for UX consistency. Even the item padding attribute you noted is meant to be informational for apps that may want to use it to style their own custom action views.




回答2:


If you wanna change text parametres like text size, color or style, you must use actionMenuTextAppearance. Here is my code for 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>

EDIT: This is my template for styling action bar with ActionBarSherlock: https://github.com/petrnohejl/Android-Templates-And-Utilities/tree/master/Res-Theme-Holo-Deprecated



来源:https://stackoverflow.com/questions/8254476/android-3-0-cannot-style-action-bar-s-action-item-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!