【问题标题】:ActionBarSherlock - Tabs appearing ABOVE actionbar with custom viewActionBarSherlock - 使用自定义视图显示在操作栏上方的选项卡
【发布时间】:2012-10-19 11:19:53
【问题描述】:

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

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

}

但是,当我隐藏徽标时,操作栏会像这样在我的标签下方移动:

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

我做错了什么?

【问题讨论】:

标签: android tabs android-actionbar actionbarsherlock


【解决方案1】:

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

在您的 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);

【讨论】:

    【解决方案2】:

    值得注意的是,所选答案仅适用于原生 ActionBar,因为元素 android.R.id.home 在 pre-hc 上不存在。也就是说,在 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);
    

    【讨论】:

    • 不过,这不允许我像我想的那样将标题用作“向上”按钮。
    • 这工作正常,但在徽标曾经存在的地方没有留下黑色空间。如何摆脱黑色空间?
    【解决方案3】:

    这也很好用! (阅读错误报告 cmets ;))

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

    【讨论】:

      【解决方案4】:

      根据@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);
      }
      

      【讨论】:

        【解决方案5】:

        上面的所有帖子都折叠了主页图标,但留下了空白。为了避免这种情况,您需要将徽标大小设置为零。下面添加了我的代码 sn-p。它可能对其他遇到同样问题的人有所帮助。谢谢

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

        【讨论】:

        • 这是最好的答案,其他解决方案会改变操作栏样式
        【解决方案6】:

        我用过这个

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

        【讨论】:

          【解决方案7】:

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

          【讨论】:

            猜你喜欢
            • 2012-08-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多