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

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),则 Action 栏会出现在正确的位置(但是带有我不想要的徽标):

我究竟做错了什么?

最佳答案

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

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

09-10 06:10
查看更多