Shaversport
Shaversport

Reputation: 13

Setting a custom view for ActionBar Sherlock tab

I'd like to draw over one of the tab icons of the actionbarsherlock.  To do this,  instead of setting a tab icon, I'm trying to create a view that will be styled exactly the same as the other ABS tabs. It looks like abs4 is adding the tabs to a ScrollingTabContainerView. The TabView class in ScrollingTabContainerView seems to be responsible for taking the icons and laying them out in the center of the tabs, so I've modeled the following code on that code found in its update() method.

Drawable icon = getResources().getDrawable(resid);
LinearLayout ll = new LinearLayout(this, null, android.R.attr.actionBarTabStyle);
ImageView iconView = new ImageView(this);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER_VERTICAL;
iconView.setLayoutParams(lp);
ll.addView(iconView, 0);
iconView.setImageDrawable(icon);

Then I call setCustomView() with ll on the Tab. When I want to write over the tab, I can do so by retrieving the view from the tab with getCustomView.

If I do not specify  the creation of the LinearLayout above with android.R.attr.actionBarTabStyle it works, but the icon is not laid out in the center of the tab.  If I set the attributes with that Id, my app crashes on startup with a NoSuchMethodError from android.widget.LinearLayout.

What's the proper way to set a style using an android attribute?

Upvotes: 1

Views: 4469

Answers (1)

Jake Wharton
Jake Wharton

Reputation: 76075

The 3-arg LinearLayout constructor was only added in API 11.

The library leverages the system layout inflater to get around this by specifying the default style in XML. You can do the same:

<LinearLayout style="?attr/actionBarTabStyle" />

and then simply:

LineraLayout tabView = LayoutInflater.from(context).inflate(R.layout.my_tab, null);
//set your layout params and setCustomView

Upvotes: 2

Related Questions