How to customize Android ActionBar to show a custom view before the tabs?

后端 未结 7 1956
孤街浪徒
孤街浪徒 2020-12-29 19:48

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 no

相关标签:
7条回答
  • 2020-12-29 20:03

    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.

    0 讨论(0)
  • 2020-12-29 20:09

    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.

    0 讨论(0)
  • 2020-12-29 20:18

    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

    0 讨论(0)
  • 2020-12-29 20:18

    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)

    0 讨论(0)
  • 2020-12-29 20:27

    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);
    
    0 讨论(0)
  • 2020-12-29 20:29

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

    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.

    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.

    Then call:

    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.

    Good luck.

    0 讨论(0)
提交回复
热议问题