1

我在我的 ABS 上使用自定义布局ActionBar,比如

    View abview = getLayoutInflater().inflate(R.layout.custombar, null);
    getSupportActionBar().setCustomView(abview);

现在没有主页/向上按钮,我已经尝试过:

getSupportActionBar().setHomeButtonEnabled(true); 

在 my 上使用自定义视图时ActionBar,我必须自己处理向上按钮吗?

4

3 回答 3

5

我遇到了同样的问题,它似乎取决于您指定的顺序。以下是我如何显示我的自定义视图,同时仍保留 home/back 操作(此代码在 中onCreateOptionsMenu):

ActionBar actionBar = getSupportActionBar();
// 1) Inflate your menu, if you have also need actions
getSupportMenuInflater().inflate(R.menu.normal_action_menu, menu);
// 2) Set your display to custom next
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
// 3) Do any other config to the action bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
// 4) Now set your custom view
actionBar.setCustomView(R.layout.custom_view);
于 2013-07-16T19:08:56.053 回答
2

如果在 ActionBar (ActionBarsherlock) 中使用自定义视图,则必须自行管理所有 actionBar。所以最好保留 ActionBar 并将 xml 设置为背景和/或在特定元素中设置 customview。您想在自定义操作栏中做什么?

于 2013-03-06T10:00:57.460 回答
1

您可以在操作栏中添加自定义操作视图,如下所示:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (DEBUG_MODE) {
        Log.d(TAG, "onCreateOptionsMenu()");
    }
    getSupportMenuInflater().inflate(R.menu.menu_generic, menu);

    // Progress
    final MenuItem progress = menu.findItem(R.id.menu_progress);
    progress.setActionView(R.layout.action_view_progress);

    mProgressText = (TextView) progress.getActionView().findViewById(R.id.total_achievement_text);

    return super.onCreateOptionsMenu(menu);
}

menu_genric.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/menu_progress"
    android:showAsAction="always"/>
</menu>

之后,您可以参考 mProgressText 增加您的数量

于 2013-03-07T13:49:46.540 回答