如何自定义Android的动作条显示标签前的自定义视图? [英] How to customize Android ActionBar to show a custom view before the tabs?

查看:98
本文介绍了如何自定义Android的动作条显示标签前的自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是与标签的动作条的自定义视图。我的问题是定制视图的排序。 Android正在显示它的标签 - 这是我不希望

I'm using a custom view for the ActionBar with Tabs. My problem is the ordering of the custom view. Android is displaying it AFTER the tabs - which I do not want.

我想自定义视图中显示的标签。

I want the custom view displayed BEFORE the tabs.

有没有办法自定义动作条显示标签前的自定义视图?或者这是不可能的?

Is there a way to customize the actionBar to show the custom view before the tabs? or is this not possible?

code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    View customActionBarView = 
        getLayoutInflater().inflate(R.layout.home_actionbar, null, true);
    ActionBar.LayoutParams lp = 
        new ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT);

    lp.gravity = Gravity.START;
    bar.setCustomView(customActionBarView, lp);
    bar.setLogo(R.drawable.logo);
    bar.setHomeButtonEnabled(true);
    bar.setDisplayShowCustomEnabled(true);

    bar.addTab(bar.newTab()
        .setText("Stuff")
        .setTabListener(new TabListener<StuffFragment>(
            this, "stuff", StuffFragment.class)));

    bar.addTab(bar.newTab()
        .setText("Friends")
        .setTabListener(new TabListener<ContactsFragment>(
            this, "friends", ContactsFragment.class)));

    bar.addTab(bar.newTab()
        .setText("Messages")
        .setTabListener(new TabListener<ConversationsFragment>(
            this, "messages", ConversationsFragment.class)));

    if (savedInstanceState != null) {
        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }

    bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | 
        ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_USE_LOGO);

    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);    
}

推荐答案

这似乎是使用选项卡和自定义视图时,是预期的行为。 的https://$c$c.google.com / P /安卓/问题/详细信息?ID = 36191#C3

This seems to be the intended behaviour when using tabs and custom views. https://code.google.com/p/android/issues/detail?id=36191#c3

如果您看一看<一href="http://stackoverflow.com/questions/12973143/actionbarsherlock-tabs-appearing-above-actionbar-with-custom-view">ActionBarSherlock - 标签出现以上动作条自定义视图很多人都遇到这个问题,以及有些人提供的解决方案。

If you take a look at ActionBarSherlock - Tabs appearing ABOVE actionbar with custom view many other people are experiencing this as well and some people have offered solutions.

我一直无法得到任何的解决方案的工作,但他们可能会为你工作。特技似乎是,以确保标志没有被设置为被隐藏。呼叫
     getActionBar()。setDisplayShowHomeEnabled(真) getSupportActionBar()。setDisplayShowHomeEnabled(真)如果使用ActionBarSherlock。

I have been unable to get any of the solutions working, but they may work for you. The trick seems to be to make sure the logo is is not set to be hidden. Call
getActionBar().setDisplayShowHomeEnabled(true) or getSupportActionBar().setDisplayShowHomeEnabled(true) if using ActionBarSherlock.

然后调用:

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

这将得到一个对包含徽标并将其设置为旷古使自定义视图以填充整个父视图的动作条的看法,但应保持标签下...

This will get a reference to the actionbar view that holds the logo and sets it to gone with enables the custom view to fill the entire parent view, but should keep the tabs underneath...

正如我所说的我无法得到这个工作,但有些人已经成功。

As I said I was unable to get this working, but some people have had success.

祝你好运。

这篇关于如何自定义Android的动作条显示标签前的自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆