ActionBarSherlock - 使用自定义视图,在actionbar上方显示选项卡

20

我想创建一个没有应用程序标志/文字,只有中央图片的Actionbar,并且我知道可以使用自定义视图来实现,以下是我的代码:

protected void initActionBar()
{
    RelativeLayout custom = new RelativeLayout(getApplicationContext());
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    custom.setLayoutParams(params); 

    custom.setGravity(Gravity.CENTER);
    custom.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_grad_grey_rounded));

    ImageView image =new ImageView(getApplicationContext());
    image.setBackgroundResource(R.drawable.ic_launcher); 
    custom.addView(image);

    ab = getSupportActionBar();


    ab.setDisplayShowCustomEnabled(true);
    ab.setCustomView(custom);
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    tab_connect = ab.newTab();
    tab_connect.setText("CONNECT");
    tab_connect.setTabListener(this);

    tab_discover = ab.newTab();
    tab_discover.setText("DISCOVER");
    tab_discover.setTabListener(this);

    tab_simplify= ab.newTab();
    tab_simplify.setText("SIMPLIFY");
    tab_simplify.setTabListener(this);

    ab.addTab(tab_simplify);
    ab.addTab(tab_discover);
    ab.addTab(tab_connect);

    ab.setDisplayShowTitleEnabled(false);
    ab.setDisplayShowHomeEnabled(false);

}

然而,当我隐藏Logo时,ActionBar就会像这样移动到选项卡下面:

screen shot

但是,如果我设置ab.setDisplayShowHomeEnabled(true),ActionBar就会出现在正确的位置(但有Logo,而我不想要它):

enter image description here

我做错了什么?


1
似乎是一个bug:https://github.com/JakeWharton/ActionBarSherlock/issues/327 - input
刚刚尝试了使用普通的Android ActionBar,但是遇到了同样的问题... - AndroidNoob
7个回答

32

以下是一个简单的解决方法:

在你的onCreate方法中使用以下代码:

View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);

这将完全折叠主页按钮。

PS: 我正在使用标准ActionBar,但这应该是一样的。

或者

如果你想支持Sherlock Actionbar,那么你必须使用这个。

actionBar.setLogo(null); // forgot why this one but it helped

View homeIcon = findViewById(
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
        android.R.id.home : R.id.abs__home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
((View) homeIcon).setVisibility(View.GONE);

actionBar.setDisplayShowTitleEnabled(false);

12
值得注意的是,所选答案仅适用于原生ActionBar,因为在pre-hc上不存在元素android.R.id.home。也就是说,在pre-hc设备上,您将会得到NullPointerException。
由于您正在使用ActionBarSherlock,我假定您想要支持pre-hc设备,所以您应该根据平台版本使用正确的ID。ABS将通过其对ActionBar的实现公开等效的android.R.id.home,即R.id.abs__home。
因此,我们可以将建议的解决方法进行调整:
View homeIcon = findViewById(
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
                    android.R.id.home : R.id.abs__home);
((View) homeIcon.getParent()).setVisibility(View.GONE);

然而,这并不允许我像我希望的那样将标题用作“向上”按钮。 - Czechnology
这个功能运行良好,但是原本标志所在的地方现在有一个黑色空格。我如何消除黑色空格? - BurninatorDor

6
这也很好用!(读一下错误报告的评论;)
actionBar.setLogo(new ColorDrawable(Color.TRANSPARENT));

3

参考@furyfred的回答,当使用AppCompat库中的ActionBar时,可以使用此代码:

View homeIcon = findViewById(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
            android.R.id.home : android.support.v7.appcompat.R.id.home);

if (null != homeIcon && null != homeIcon.getParent())
{
    ((View) homeIcon.getParent()).setVisibility(View.GONE);
}

2
所有的帖子都会使主页图标折叠,但是留下了空白的空间。为了避免这种情况,您需要将标志大小设置为零。下面是我的代码片段,它可能对其他遇到同样问题的人有所帮助。谢谢。
       actionBar.setLogo(new ColorDrawable(Color.TRANSPARENT));

      View homeIcon = findViewById(
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
                android.R.id.home : R.id.abs__home);
        ((View) homeIcon.getParent()).setLayoutParams(new LinearLayout.LayoutParams(0, 0));
        ((View) homeIcon).setVisibility(View.GONE);

这是最好的答案,其他解决方案会改变操作栏的样式。 - marmor

1

我使用了这个

ActionBar ab = getSupportActionBar();
ab.setDisplayShowHomeEnabled(true);
    ab.setDisplayHomeAsUpEnabled(false);
    ab.setHomeButtonEnabled(false);
    ab.setLogo(null);

    View homeIcon = findViewById(
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
            android.R.id.home : R.id.abs__home);
    ((View) homeIcon.getParent()).setVisibility(View.GONE);
    ((View) homeIcon).setVisibility(View.GONE);

    ab.setDisplayShowTitleEnabled(false);

0

只需从您的actionBar.setDisplayOptions()方法中删除DISPLAY_SHOW_HOME标志即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接