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

时间:2012-10-19 11:19:54

标签: android tabs android-actionbar actionbarsherlock

我正在尝试创建一个没有应用程序徽标/文本的动作栏,并且有一个集中的图片,我知道可以使用自定义视图,这是我的代码:

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);

}

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

screen shot

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

enter image description here

我做错了什么?

7 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

答案 2 :(得分:6)

这很有用! (阅读错误报告评论;))

actionBar.setLogo(new ColorDrawable(Color.TRANSPARENT));

答案 3 :(得分:3)

关注@furyfred回答,使用ActionBar库中的AppCompat时可以使用此代码:

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);
}

答案 4 :(得分: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);

答案 5 :(得分: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);

答案 6 :(得分:0)

从actionBar.setDisplayOptions()方法中删除DISPLAY_SHOW_HOME标志。