ActionBarSherlock自定义ActionBar布局 - 主页图标消失了

时间:2013-03-06 09:02:47

标签: android actionbarsherlock

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

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

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

getSupportActionBar().setHomeButtonEnabled(true); 

在我的ActionBar上使用自定义视图时,我是否必须自己处理完所有按钮?

3 个答案:

答案 0 :(得分:5)

我遇到了同样的问题,它似乎取决于你指定的顺序。以下是我如何显示自定义视图,同时仍保留主页/后退操作(此代码位于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);

答案 1 :(得分:2)

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

答案 2 :(得分: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增加您的号码

相关问题