So I am looking at the action bar and the icons look too far down. I've checked everywhere can can't figure out where the offset or padding is coming from. It's probably because recently I switched to using a Toolbar in the layout instead of the regular action bar, but I can't go back even if I wanted to.
After checking all the padding, margins, translations on the toolbar and coming up empty, I bring up the "now I mean business tool": the hierarchy viewer.
Sure enough I can see that the icons have some kind of offset.
The hamburger icon is too low. What about the action bar itself?
Nope, action bar is right on. What about the menus?
Yep, their too low too.
My options are looking dim. Either I add an offset myself to compensate for the mysterious extra padding or I throw out the Toolbar all together. I know that the answer has to be somewhere so I start checking the Android source as a Hail Mary. I guess that the only other thing that could be doing it would be the Toolbars onLayout().
Something is suspicious about the getChildTop:
private int getChildTop(View child, int alignmentHeight) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int childHeight = child.getMeasuredHeight();
final int alignmentOffset = alignmentHeight > 0 ? (childHeight - alignmentHeight) / 2 : 0;
switch (getChildVerticalGravity(lp.gravity)) {
case Gravity.TOP:
return getPaddingTop() - alignmentOffset;
// ...
Where is that alignmentHeight coming from? It turns out it's coming from Toolbar.minHeight. Now that code looks like a bug. It shouldn't be trying to center the icon when the gravity is TOP and it's already greater than minHeight.
Android being open source really comes in handy. I can see that to bypass the bug I just need to set minHeight to 0.
// There is a bug in Toolbar where minHeight is being used incorrectly to create an offset when // laying out the children. So we use 0 for minHeight so that the bug is not exposed. mActionBarToolbar.setMinimumHeight(0);Let's see the results.
The icons look better centered.
And in fact are.
Looks like it's going to be a great day after all.




No comments:
Post a Comment