Android: it was caused by Layout's BaselineAligned,
This question comes from a netizen's question http://ask.csdn.net/questions/206909#answer_140060
View the layout file below
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". mainActivity "> <TextView android: layout_width =" 150dp "android: layout_height =" 60dp "android: background =" # 8f8f8f "android: text = "first"/> <TextView android: layout_width = "100dp" android: layout_height = "60dp" android: background = "#8f00" android: gravity = "center" android: text = "second" android: textSize = "20dp"/> </LinearLayout>
This layout is very simple. Two TextView components are nested in LinearLayout. They have the same height and different width. The expected result is that the top of the two textviews are aligned, with the same height and size, the text alignment mode can be set as needed. The structure actually rendered by this layout is as follows:
If you set different font sizes and Gravity, the location will be disordered. The reason is that the BaselineAligned attribute of LinearLayout has caused you to take a look at the default attributes of LinearLayout.
Most of the time, we are confused by the Green Box. For the boolean attribute, this status is not necessarily false. It only indicates that it is consistent with the default value set by the system, so how does the system set this variable? Let's look at the LinearLayout source code (Location: \ sdks \ sources \ android-19 \ android \ widget)
/** * Whether the children of this layout are baseline aligned. Only applicable * if {@link #mOrientation} is horizontal. */ @ViewDebug.ExportedProperty(category = "layout") private boolean mBaselineAligned = true;
The default value is true.
Horizontal LayoutValid. So what is the impact of this attribute on the layout? continue to look at what the LinearLayout layout has done:
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (mOrientation == VERTICAL) { layoutVertical(l, t, r, b); } else { layoutHorizontal(l, t, r, b); } }
LayoutHorizontal is called during horizontal layout.
if (child == null) { childLeft += measureNullChild(childIndex); } else if (child.getVisibility() != GONE) { final int childWidth = child.getMeasuredWidth(); final int childHeight = child.getMeasuredHeight(); int childBaseline = -1; final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); if (baselineAligned && lp.height != LayoutParams.MATCH_PARENT) { childBaseline = child.getBaseline(); } int gravity = lp.gravity; if (gravity < 0) { gravity = minorGravity; } switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) { case Gravity.TOP: childTop = paddingTop + lp.topMargin; if (childBaseline != -1) { childTop += maxAscent[INDEX_TOP] - childBaseline; } break; case Gravity.CENTER_VERTICAL: // Removed support for baseline alignment when layout_gravity or // gravity == center_vertical. See bug #1038483. // Keep the code around if we need to re-enable this feature // if (childBaseline != -1) { // // Align baselines vertically only if the child is smaller than us // if (childSpace - childHeight > 0) { // childTop = paddingTop + (childSpace / 2) - childBaseline; // } else { // childTop = paddingTop + (childSpace - childHeight) / 2; // } // } else { childTop = paddingTop + ((childSpace - childHeight) / 2) + lp.topMargin - lp.bottomMargin; break; case Gravity.BOTTOM: childTop = childBottom - childHeight - lp.bottomMargin; if (childBaseline != -1) { int descent = child.getMeasuredHeight() - childBaseline; childTop -= (maxDescent[INDEX_BOTTOM] - descent); } break; default: childTop = paddingTop; break; } if (hasDividerBeforeChildAt(childIndex)) { childLeft += mDividerWidth; } childLeft += lp.leftMargin; setChildFrame(child, childLeft + getLocationOffset(child), childTop, childWidth, childHeight); childLeft += childWidth + lp.rightMargin + getNextLocationOffset(child); i += getChildrenSkipCount(child, childIndex); }
We can see baselineAligned in the code segment of else... if:
if (baselineAligned && lp.height != LayoutParams.MATCH_PARENT)
If the BaseLine alignment is performed and the height of the layout parameter is not set to match_parent, the BaseLine of chile is obtained. Here is the BaseLine value of TextView. After calculation of some columns
setChildFrame(child, childLeft + getLocationOffset(child), childTop, childWidth, childHeight);
OnLayout for child
private void setChildFrame(View child, int left, int top, int width, int height) { child.layout(left, top, left + width, top + height); }
Let's take a look at the getBaseline of TextView.
@Override public int getBaseline() { if (mLayout == null) { return super.getBaseline(); } int voffset = 0; if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) { voffset = getVerticalOffset(true); } if (isLayoutModeOptical(mParent)) { voffset -= getOpticalInsets().top; } return getExtendedPaddingTop() + voffset + mLayout.getLineBaseline(0); }
If it is not Gravity. TOP, you must use getVerticalOffset to calculate the voffset value.
int getVerticalOffset(boolean forceNormal) { int voffset = 0; final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; Layout l = mLayout; if (!forceNormal && mText.length() == 0 && mHintLayout != null) { l = mHintLayout; } if (gravity != Gravity.TOP) { int boxht = getBoxHeight(l); int textht = l.getHeight(); if (textht < boxht) { if (gravity == Gravity.BOTTOM) voffset = boxht - textht; else // (gravity == Gravity.CENTER_VERTICAL) voffset = (boxht - textht) >> 1; } } return voffset; }
This method calculates a vertical offset value, which directly affects the childTop value in Layout, resulting in Layout confusion and baseline alignment, that is, the text of multiple textviews is aligned, regardless of top and bottom.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.