ActionBarSherlock - 使用自定义视图显示在ABOVE操作栏上的选项卡

And*_*oob 20 tabs android actionbarsherlock android-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);

}
Run Code Online (Sandbox Code Playgroud)

但是,当我隐藏徽标时,操作栏会移动到我的标签下方,如下所示:

屏幕截图

但如果我设置了ab.setDisplayShowHomeEnabled(true),则操作栏会出现在它的正确位置(但带有我不想要的徽标):

在此输入图像描述

我究竟做错了什么?

小智 32

这是一个简单的解决方法:

在onCreate方法中使用以下内容:

View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

这完全折叠了主页按钮.

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);
Run Code Online (Sandbox Code Playgroud)


J. *_*vea 12

值得注意的是,所选答案仅适用于本机ActionBar,因为pre.hc上不存在元素android.R.id.home.也就是说,在pre-hc设备上,您将获得NPE.

由于您使用的是ActionBarSherlock,我假设您要支持pre-hc设备,因此您应该根据平台版本使用正确的ID.ABS将通过R.id.abs__home暴露相当于android.R.id.home的ActionBar实现.

因此,我们可以将建议的解决方法调整为:

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);
Run Code Online (Sandbox Code Playgroud)


小智 6

这也很棒!(阅读bugreport评论;))

actionBar.setLogo(new ColorDrawable(Color.TRANSPARENT));
Run Code Online (Sandbox Code Playgroud)