How to customize Android ActionBar to show a custo

2019-02-04 05:39发布

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

enter image description here

7条回答
等我变得足够好
2楼-- · 2019-02-04 05:53

First set "DisplayShowHomeEnabled" property of actionbar to "true": actionBar.setDisplayShowHomeEnabled(true);

and then:

    View homeIcon = findViewById(android.R.id.home);
        ((View) homeIcon.getParent()).setVisibility(View.GONE);
        ((View) homeIcon).setVisibility(View.GONE);

I hope it helps :)

查看更多
Lonely孤独者°
3楼-- · 2019-02-04 05:55

I had the same problem and i figured out a way to solve it. It's not an "elegant" solution but it was the best i could find. As first thing since you want to modify the standard ActionBar behaviour you have to force ActionBar Sherlok to always use the non native implementation. To do that open ActionBarSherlok.java and comment this line of code:

registerImplementation(ActionBarSherlockNative.class);

then remove all the values-v11 values-v14 etc and be sure to always extend Theme.Sherlock and never Theme.Holo

At this point you are sure that the ActionBar implementation is always the one written by Jake Wharton. The only thing left to do is make the ActionBar view layout the way you want. Open ActionBarView.java and in the onLayout() method move this piece of code

 if (mExpandedActionView == null) { 
        final boolean showTitle = mTitleLayout != null && mTitleLayout.getVisibility() != GONE &&
                (mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0;
        if (showTitle) {
            x += positionChild(mTitleLayout, x, y, contentHeight);
        }

        switch (mNavigationMode) {
            case ActionBar.NAVIGATION_MODE_STANDARD:
                break;
            case ActionBar.NAVIGATION_MODE_LIST:
                if (mListNavLayout != null) {
                    if (showTitle) x += mItemPadding;
                    x += positionChild(mListNavLayout, x, y, contentHeight) + mItemPadding;
                }
                break;
            case ActionBar.NAVIGATION_MODE_TABS:
                if (mTabScrollView != null) {
                    if (showTitle) x += mItemPadding;
                    x += positionChild(mTabScrollView, x, y, contentHeight) + mItemPadding;
                }
                break;
        }
    }

right before this piece

if (mProgressView != null) {
        mProgressView.bringToFront();
        final int halfProgressHeight = mProgressView.getMeasuredHeight() / 2;
        mProgressView.layout(mProgressBarPadding, -halfProgressHeight,
                mProgressBarPadding + mProgressView.getMeasuredWidth(), halfProgressHeight);
    }

You're done! Hope this helps

查看更多
趁早两清
4楼-- · 2019-02-04 06:01

use ActionBarSherlock, which is very good implementation of Custom ActionBar for all android versions and very easy to use.

or you can create your own ActionBar using custom title bar and and its fair enough easy to implement. You can see this and this examples are very good examples of custom title bars.

查看更多
我想做一个坏孩纸
5楼-- · 2019-02-04 06:01

Are you making an app for the tablet? As far as I know, the tabs bar of actionbar basically appears below the it on cellphone.
If on tablet, I'm afraid you can't adjust the positions of tabs. what I can think of is that you need to quit using navigation mode and make a custom view which look like tabs to replace the actionBar tabs. Of course this causes a lot of extra effort to deal with the navigation stuff.

查看更多
SAY GOODBYE
6楼-- · 2019-02-04 06:03

Just create a custom view for your home button. In this view you can combine both logo and the custom view you're trying to position on left. Then add tabs as normal

The only downside of this solution is that you'll need to maintain the state of the back button yourself (if you use it at all)

查看更多
爷、活的狠高调
7楼-- · 2019-02-04 06:08

When you add your custom view to the ActionBar you can specify the gravity also.

ActionBar.LayoutParams lp = new ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);              
lp.gravity = Gravity.LEFT;
customView.setLayoutParams(lp);
查看更多
登录 后发表回答