| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.res.TypedArray; |
| 25 |
import android.graphics.Canvas; |
| 26 |
import android.graphics.drawable.Drawable; |
| 27 |
import android.os.Build; |
| 28 |
import android.util.AttributeSet; |
| 29 |
import android.view.Gravity; |
| 30 |
import android.view.View; |
| 31 |
import android.view.ViewDebug; |
| 32 |
import android.view.ViewGroup; |
| 33 |
import android.view.ViewHierarchyEncoder; |
| 34 |
import android.view.inspector.InspectableProperty; |
| 35 |
import android.widget.RemoteViews.RemoteView; |
| 36 |
|
| 37 |
import com.android.internal.R; |
| 38 |
|
| 39 |
import java.lang.annotation.Retention; |
| 40 |
import java.lang.annotation.RetentionPolicy; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
@RemoteView |
| 90 |
public class LinearLayout extends ViewGroup { |
| 91 |
|
| 92 |
@IntDef({HORIZONTAL, VERTICAL}) |
| 93 |
@Retention(RetentionPolicy.SOURCE) |
| 94 |
public @interface OrientationMode {} |
| 95 |
|
| 96 |
public static final int HORIZONTAL = 0; |
| 97 |
public static final int VERTICAL = 1; |
| 98 |
|
| 99 |
|
| 100 |
@IntDef(flag = true, prefix = { "SHOW_DIVIDER_" }, value = { |
| 101 |
SHOW_DIVIDER_NONE, |
| 102 |
SHOW_DIVIDER_BEGINNING, |
| 103 |
SHOW_DIVIDER_MIDDLE, |
| 104 |
SHOW_DIVIDER_END |
| 105 |
}) |
| 106 |
@Retention(RetentionPolicy.SOURCE) |
| 107 |
public @interface DividerMode {} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public static final int SHOW_DIVIDER_NONE = 0; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public static final int SHOW_DIVIDER_BEGINNING = 1; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public static final int SHOW_DIVIDER_MIDDLE = 2; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static final int SHOW_DIVIDER_END = 4; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
private final boolean mAllowInconsistentMeasurement; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
@ViewDebug.ExportedProperty(category = "layout") |
| 138 |
private boolean mBaselineAligned = true; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
@ViewDebug.ExportedProperty(category = "layout") |
| 148 |
private int mBaselineAlignedChildIndex = -1; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
@ViewDebug.ExportedProperty(category = "measurement") |
| 156 |
private int mBaselineChildTop = 0; |
| 157 |
|
| 158 |
@ViewDebug.ExportedProperty(category = "measurement") |
| 159 |
private int mOrientation; |
| 160 |
|
| 161 |
@ViewDebug.ExportedProperty(category = "measurement", flagMapping = { |
| 162 |
@ViewDebug.FlagToString(mask = -1, |
| 163 |
equals = -1, name = "NONE"), |
| 164 |
@ViewDebug.FlagToString(mask = Gravity.NO_GRAVITY, |
| 165 |
equals = Gravity.NO_GRAVITY,name = "NONE"), |
| 166 |
@ViewDebug.FlagToString(mask = Gravity.TOP, |
| 167 |
equals = Gravity.TOP, name = "TOP"), |
| 168 |
@ViewDebug.FlagToString(mask = Gravity.BOTTOM, |
| 169 |
equals = Gravity.BOTTOM, name = "BOTTOM"), |
| 170 |
@ViewDebug.FlagToString(mask = Gravity.LEFT, |
| 171 |
equals = Gravity.LEFT, name = "LEFT"), |
| 172 |
@ViewDebug.FlagToString(mask = Gravity.RIGHT, |
| 173 |
equals = Gravity.RIGHT, name = "RIGHT"), |
| 174 |
@ViewDebug.FlagToString(mask = Gravity.START, |
| 175 |
equals = Gravity.START, name = "START"), |
| 176 |
@ViewDebug.FlagToString(mask = Gravity.END, |
| 177 |
equals = Gravity.END, name = "END"), |
| 178 |
@ViewDebug.FlagToString(mask = Gravity.CENTER_VERTICAL, |
| 179 |
equals = Gravity.CENTER_VERTICAL, name = "CENTER_VERTICAL"), |
| 180 |
@ViewDebug.FlagToString(mask = Gravity.FILL_VERTICAL, |
| 181 |
equals = Gravity.FILL_VERTICAL, name = "FILL_VERTICAL"), |
| 182 |
@ViewDebug.FlagToString(mask = Gravity.CENTER_HORIZONTAL, |
| 183 |
equals = Gravity.CENTER_HORIZONTAL, name = "CENTER_HORIZONTAL"), |
| 184 |
@ViewDebug.FlagToString(mask = Gravity.FILL_HORIZONTAL, |
| 185 |
equals = Gravity.FILL_HORIZONTAL, name = "FILL_HORIZONTAL"), |
| 186 |
@ViewDebug.FlagToString(mask = Gravity.CENTER, |
| 187 |
equals = Gravity.CENTER, name = "CENTER"), |
| 188 |
@ViewDebug.FlagToString(mask = Gravity.FILL, |
| 189 |
equals = Gravity.FILL, name = "FILL"), |
| 190 |
@ViewDebug.FlagToString(mask = Gravity.RELATIVE_LAYOUT_DIRECTION, |
| 191 |
equals = Gravity.RELATIVE_LAYOUT_DIRECTION, name = "RELATIVE") |
| 192 |
}, formatToHexString = true) |
| 193 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 194 |
private int mGravity = Gravity.START | Gravity.TOP; |
| 195 |
|
| 196 |
@ViewDebug.ExportedProperty(category = "measurement") |
| 197 |
@UnsupportedAppUsage |
| 198 |
private int mTotalLength; |
| 199 |
|
| 200 |
@ViewDebug.ExportedProperty(category = "layout") |
| 201 |
private float mWeightSum; |
| 202 |
|
| 203 |
@ViewDebug.ExportedProperty(category = "layout") |
| 204 |
@UnsupportedAppUsage |
| 205 |
private boolean mUseLargestChild; |
| 206 |
|
| 207 |
@UnsupportedAppUsage |
| 208 |
private int[] mMaxAscent; |
| 209 |
@UnsupportedAppUsage |
| 210 |
private int[] mMaxDescent; |
| 211 |
|
| 212 |
private static final int VERTICAL_GRAVITY_COUNT = 4; |
| 213 |
|
| 214 |
private static final int INDEX_CENTER_VERTICAL = 0; |
| 215 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 216 |
private static final int INDEX_TOP = 1; |
| 217 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 218 |
private static final int INDEX_BOTTOM = 2; |
| 219 |
private static final int INDEX_FILL = 3; |
| 220 |
|
| 221 |
@UnsupportedAppUsage |
| 222 |
private Drawable mDivider; |
| 223 |
private int mDividerWidth; |
| 224 |
private int mDividerHeight; |
| 225 |
private int mShowDividers; |
| 226 |
private int mDividerPadding; |
| 227 |
|
| 228 |
private int mLayoutDirection = View.LAYOUT_DIRECTION_UNDEFINED; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
private static boolean sCompatibilityDone = false; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
private static boolean sRemeasureWeightedChildren = true; |
| 240 |
|
| 241 |
public LinearLayout(Context context) { |
| 242 |
this(context, null); |
| 243 |
} |
| 244 |
|
| 245 |
public LinearLayout(Context context, @Nullable AttributeSet attrs) { |
| 246 |
this(context, attrs, 0); |
| 247 |
} |
| 248 |
|
| 249 |
public LinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 250 |
this(context, attrs, defStyleAttr, 0); |
| 251 |
} |
| 252 |
|
| 253 |
public LinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 254 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 255 |
|
| 256 |
if (!sCompatibilityDone && context != null) { |
| 257 |
final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion; |
| 258 |
|
| 259 |
|
| 260 |
sRemeasureWeightedChildren = targetSdkVersion >= Build.VERSION_CODES.P; |
| 261 |
|
| 262 |
sCompatibilityDone = true; |
| 263 |
} |
| 264 |
|
| 265 |
final TypedArray a = context.obtainStyledAttributes( |
| 266 |
attrs, com.android.internal.R.styleable.LinearLayout, defStyleAttr, defStyleRes); |
| 267 |
saveAttributeDataForStyleable(context, com.android.internal.R.styleable.LinearLayout, |
| 268 |
attrs, a, defStyleAttr, defStyleRes); |
| 269 |
|
| 270 |
int index = a.getInt(com.android.internal.R.styleable.LinearLayout_orientation, -1); |
| 271 |
if (index >= 0) { |
| 272 |
setOrientation(index); |
| 273 |
} |
| 274 |
|
| 275 |
index = a.getInt(com.android.internal.R.styleable.LinearLayout_gravity, -1); |
| 276 |
if (index >= 0) { |
| 277 |
setGravity(index); |
| 278 |
} |
| 279 |
|
| 280 |
boolean baselineAligned = a.getBoolean(R.styleable.LinearLayout_baselineAligned, true); |
| 281 |
if (!baselineAligned) { |
| 282 |
setBaselineAligned(baselineAligned); |
| 283 |
} |
| 284 |
|
| 285 |
mWeightSum = a.getFloat(R.styleable.LinearLayout_weightSum, -1.0f); |
| 286 |
|
| 287 |
mBaselineAlignedChildIndex = |
| 288 |
a.getInt(com.android.internal.R.styleable.LinearLayout_baselineAlignedChildIndex, -1); |
| 289 |
|
| 290 |
mUseLargestChild = a.getBoolean(R.styleable.LinearLayout_measureWithLargestChild, false); |
| 291 |
|
| 292 |
mShowDividers = a.getInt(R.styleable.LinearLayout_showDividers, SHOW_DIVIDER_NONE); |
| 293 |
mDividerPadding = a.getDimensionPixelSize(R.styleable.LinearLayout_dividerPadding, 0); |
| 294 |
setDividerDrawable(a.getDrawable(R.styleable.LinearLayout_divider)); |
| 295 |
|
| 296 |
final int version = context.getApplicationInfo().targetSdkVersion; |
| 297 |
mAllowInconsistentMeasurement = version <= Build.VERSION_CODES.M; |
| 298 |
|
| 299 |
a.recycle(); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
private boolean isShowingDividers() { |
| 307 |
return (mShowDividers != SHOW_DIVIDER_NONE) && (mDivider != null); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
public void setShowDividers(@DividerMode int showDividers) { |
| 318 |
if (showDividers == mShowDividers) { |
| 319 |
return; |
| 320 |
} |
| 321 |
mShowDividers = showDividers; |
| 322 |
|
| 323 |
setWillNotDraw(!isShowingDividers()); |
| 324 |
requestLayout(); |
| 325 |
} |
| 326 |
|
| 327 |
@Override |
| 328 |
public boolean shouldDelayChildPressedState() { |
| 329 |
return false; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
@DividerMode |
| 337 |
public int getShowDividers() { |
| 338 |
return mShowDividers; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
@InspectableProperty(name = "divider") |
| 349 |
public Drawable getDividerDrawable() { |
| 350 |
return mDivider; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public void setDividerDrawable(Drawable divider) { |
| 363 |
if (divider == mDivider) { |
| 364 |
return; |
| 365 |
} |
| 366 |
mDivider = divider; |
| 367 |
if (divider != null) { |
| 368 |
mDividerWidth = divider.getIntrinsicWidth(); |
| 369 |
mDividerHeight = divider.getIntrinsicHeight(); |
| 370 |
} else { |
| 371 |
mDividerWidth = 0; |
| 372 |
mDividerHeight = 0; |
| 373 |
} |
| 374 |
|
| 375 |
setWillNotDraw(!isShowingDividers()); |
| 376 |
requestLayout(); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public void setDividerPadding(int padding) { |
| 391 |
if (padding == mDividerPadding) { |
| 392 |
return; |
| 393 |
} |
| 394 |
mDividerPadding = padding; |
| 395 |
|
| 396 |
if (isShowingDividers()) { |
| 397 |
requestLayout(); |
| 398 |
invalidate(); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public int getDividerPadding() { |
| 410 |
return mDividerPadding; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public int getDividerWidth() { |
| 419 |
return mDividerWidth; |
| 420 |
} |
| 421 |
|
| 422 |
@Override |
| 423 |
protected void onDraw(Canvas canvas) { |
| 424 |
if (mDivider == null) { |
| 425 |
return; |
| 426 |
} |
| 427 |
|
| 428 |
if (mOrientation == VERTICAL) { |
| 429 |
drawDividersVertical(canvas); |
| 430 |
} else { |
| 431 |
drawDividersHorizontal(canvas); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
void drawDividersVertical(Canvas canvas) { |
| 436 |
final int count = getVirtualChildCount(); |
| 437 |
for (int i = 0; i < count; i++) { |
| 438 |
final View child = getVirtualChildAt(i); |
| 439 |
if (child != null && child.getVisibility() != GONE) { |
| 440 |
if (hasDividerBeforeChildAt(i)) { |
| 441 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 442 |
final int top = child.getTop() - lp.topMargin - mDividerHeight; |
| 443 |
drawHorizontalDivider(canvas, top); |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
if (hasDividerBeforeChildAt(count)) { |
| 449 |
final View child = getLastNonGoneChild(); |
| 450 |
int bottom = 0; |
| 451 |
if (child == null) { |
| 452 |
bottom = getHeight() - getPaddingBottom() - mDividerHeight; |
| 453 |
} else { |
| 454 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 455 |
bottom = child.getBottom() + lp.bottomMargin; |
| 456 |
} |
| 457 |
drawHorizontalDivider(canvas, bottom); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
private View getLastNonGoneChild() { |
| 466 |
for (int i = getVirtualChildCount() - 1; i >= 0; i--) { |
| 467 |
final View child = getVirtualChildAt(i); |
| 468 |
if (child != null && child.getVisibility() != GONE) { |
| 469 |
return child; |
| 470 |
} |
| 471 |
} |
| 472 |
return null; |
| 473 |
} |
| 474 |
|
| 475 |
void drawDividersHorizontal(Canvas canvas) { |
| 476 |
final int count = getVirtualChildCount(); |
| 477 |
final boolean isLayoutRtl = isLayoutRtl(); |
| 478 |
for (int i = 0; i < count; i++) { |
| 479 |
final View child = getVirtualChildAt(i); |
| 480 |
if (child != null && child.getVisibility() != GONE) { |
| 481 |
if (hasDividerBeforeChildAt(i)) { |
| 482 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 483 |
final int position; |
| 484 |
if (isLayoutRtl) { |
| 485 |
position = child.getRight() + lp.rightMargin; |
| 486 |
} else { |
| 487 |
position = child.getLeft() - lp.leftMargin - mDividerWidth; |
| 488 |
} |
| 489 |
drawVerticalDivider(canvas, position); |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
if (hasDividerBeforeChildAt(count)) { |
| 495 |
final View child = getLastNonGoneChild(); |
| 496 |
int position; |
| 497 |
if (child == null) { |
| 498 |
if (isLayoutRtl) { |
| 499 |
position = getPaddingLeft(); |
| 500 |
} else { |
| 501 |
position = getWidth() - getPaddingRight() - mDividerWidth; |
| 502 |
} |
| 503 |
} else { |
| 504 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 505 |
if (isLayoutRtl) { |
| 506 |
position = child.getLeft() - lp.leftMargin - mDividerWidth; |
| 507 |
} else { |
| 508 |
position = child.getRight() + lp.rightMargin; |
| 509 |
} |
| 510 |
} |
| 511 |
drawVerticalDivider(canvas, position); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
void drawHorizontalDivider(Canvas canvas, int top) { |
| 516 |
mDivider.setBounds(getPaddingLeft() + mDividerPadding, top, |
| 517 |
getWidth() - getPaddingRight() - mDividerPadding, top + mDividerHeight); |
| 518 |
mDivider.draw(canvas); |
| 519 |
} |
| 520 |
|
| 521 |
void drawVerticalDivider(Canvas canvas, int left) { |
| 522 |
mDivider.setBounds(left, getPaddingTop() + mDividerPadding, |
| 523 |
left + mDividerWidth, getHeight() - getPaddingBottom() - mDividerPadding); |
| 524 |
mDivider.draw(canvas); |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
@InspectableProperty |
| 534 |
public boolean isBaselineAligned() { |
| 535 |
return mBaselineAligned; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
@android.view.RemotableViewMethod |
| 548 |
public void setBaselineAligned(boolean baselineAligned) { |
| 549 |
mBaselineAligned = baselineAligned; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
@InspectableProperty(name = "measureWithLargestChild") |
| 563 |
public boolean isMeasureWithLargestChildEnabled() { |
| 564 |
return mUseLargestChild; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
@android.view.RemotableViewMethod |
| 580 |
public void setMeasureWithLargestChildEnabled(boolean enabled) { |
| 581 |
mUseLargestChild = enabled; |
| 582 |
} |
| 583 |
|
| 584 |
@Override |
| 585 |
public int getBaseline() { |
| 586 |
if (mBaselineAlignedChildIndex < 0) { |
| 587 |
return super.getBaseline(); |
| 588 |
} |
| 589 |
|
| 590 |
if (getChildCount() <= mBaselineAlignedChildIndex) { |
| 591 |
throw new RuntimeException("mBaselineAlignedChildIndex of LinearLayout " |
| 592 |
+ "set to an index that is out of bounds."); |
| 593 |
} |
| 594 |
|
| 595 |
final View child = getChildAt(mBaselineAlignedChildIndex); |
| 596 |
final int childBaseline = child.getBaseline(); |
| 597 |
|
| 598 |
if (childBaseline == -1) { |
| 599 |
if (mBaselineAlignedChildIndex == 0) { |
| 600 |
|
| 601 |
return -1; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
throw new RuntimeException("mBaselineAlignedChildIndex of LinearLayout " |
| 606 |
+ "points to a View that doesn't know how to get its baseline."); |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
int childTop = mBaselineChildTop; |
| 616 |
|
| 617 |
if (mOrientation == VERTICAL) { |
| 618 |
final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 619 |
if (majorGravity != Gravity.TOP) { |
| 620 |
switch (majorGravity) { |
| 621 |
case Gravity.BOTTOM: |
| 622 |
childTop = mBottom - mTop - mPaddingBottom - mTotalLength; |
| 623 |
break; |
| 624 |
|
| 625 |
case Gravity.CENTER_VERTICAL: |
| 626 |
childTop += ((mBottom - mTop - mPaddingTop - mPaddingBottom) - |
| 627 |
mTotalLength) / 2; |
| 628 |
break; |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); |
| 634 |
return childTop + lp.topMargin + childBaseline; |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
@InspectableProperty |
| 643 |
public int getBaselineAlignedChildIndex() { |
| 644 |
return mBaselineAlignedChildIndex; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
@android.view.RemotableViewMethod |
| 654 |
public void setBaselineAlignedChildIndex(int i) { |
| 655 |
if ((i < 0) || (i >= getChildCount())) { |
| 656 |
throw new IllegalArgumentException("base aligned child index out " |
| 657 |
+ "of range (0, " + getChildCount() + ")"); |
| 658 |
} |
| 659 |
mBaselineAlignedChildIndex = i; |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
@Nullable |
| 672 |
View getVirtualChildAt(int index) { |
| 673 |
return getChildAt(index); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
int getVirtualChildCount() { |
| 686 |
return getChildCount(); |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
@InspectableProperty |
| 697 |
public float getWeightSum() { |
| 698 |
return mWeightSum; |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
@android.view.RemotableViewMethod |
| 714 |
public void setWeightSum(float weightSum) { |
| 715 |
mWeightSum = Math.max(0.0f, weightSum); |
| 716 |
} |
| 717 |
|
| 718 |
@Override |
| 719 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 720 |
if (mOrientation == VERTICAL) { |
| 721 |
measureVertical(widthMeasureSpec, heightMeasureSpec); |
| 722 |
} else { |
| 723 |
measureHorizontal(widthMeasureSpec, heightMeasureSpec); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
protected boolean hasDividerBeforeChildAt(int childIndex) { |
| 735 |
if (mShowDividers == SHOW_DIVIDER_NONE) { |
| 736 |
|
| 737 |
return false; |
| 738 |
} |
| 739 |
if (childIndex == getVirtualChildCount()) { |
| 740 |
|
| 741 |
return (mShowDividers & SHOW_DIVIDER_END) != 0; |
| 742 |
} |
| 743 |
boolean allViewsAreGoneBefore = allViewsAreGoneBefore(childIndex); |
| 744 |
if (allViewsAreGoneBefore) { |
| 745 |
|
| 746 |
return (mShowDividers & SHOW_DIVIDER_BEGINNING) != 0; |
| 747 |
} else { |
| 748 |
return (mShowDividers & SHOW_DIVIDER_MIDDLE) != 0; |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
private boolean hasDividerAfterChildAt(int childIndex) { |
| 759 |
if (mShowDividers == SHOW_DIVIDER_NONE) { |
| 760 |
|
| 761 |
return false; |
| 762 |
} |
| 763 |
if (allViewsAreGoneAfter(childIndex)) { |
| 764 |
|
| 765 |
return (mShowDividers & SHOW_DIVIDER_END) != 0; |
| 766 |
} |
| 767 |
return (mShowDividers & SHOW_DIVIDER_MIDDLE) != 0; |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
private boolean allViewsAreGoneBefore(int childIndex) { |
| 774 |
for (int i = childIndex - 1; i >= 0; i--) { |
| 775 |
final View child = getVirtualChildAt(i); |
| 776 |
if (child != null && child.getVisibility() != GONE) { |
| 777 |
return false; |
| 778 |
} |
| 779 |
} |
| 780 |
return true; |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
private boolean allViewsAreGoneAfter(int childIndex) { |
| 787 |
final int count = getVirtualChildCount(); |
| 788 |
for (int i = childIndex + 1; i < count; i++) { |
| 789 |
final View child = getVirtualChildAt(i); |
| 790 |
if (child != null && child.getVisibility() != GONE) { |
| 791 |
return false; |
| 792 |
} |
| 793 |
} |
| 794 |
return true; |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
void measureVertical(int widthMeasureSpec, int heightMeasureSpec) { |
| 809 |
mTotalLength = 0; |
| 810 |
int maxWidth = 0; |
| 811 |
int childState = 0; |
| 812 |
int alternativeMaxWidth = 0; |
| 813 |
int weightedMaxWidth = 0; |
| 814 |
boolean allFillParent = true; |
| 815 |
float totalWeight = 0; |
| 816 |
|
| 817 |
final int count = getVirtualChildCount(); |
| 818 |
|
| 819 |
final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 820 |
final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 821 |
|
| 822 |
boolean matchWidth = false; |
| 823 |
boolean skippedMeasure = false; |
| 824 |
|
| 825 |
final int baselineChildIndex = mBaselineAlignedChildIndex; |
| 826 |
final boolean useLargestChild = mUseLargestChild; |
| 827 |
|
| 828 |
int largestChildHeight = Integer.MIN_VALUE; |
| 829 |
int consumedExcessSpace = 0; |
| 830 |
|
| 831 |
int nonSkippedChildCount = 0; |
| 832 |
|
| 833 |
|
| 834 |
for (int i = 0; i < count; ++i) { |
| 835 |
final View child = getVirtualChildAt(i); |
| 836 |
if (child == null) { |
| 837 |
mTotalLength += measureNullChild(i); |
| 838 |
continue; |
| 839 |
} |
| 840 |
|
| 841 |
if (child.getVisibility() == View.GONE) { |
| 842 |
i += getChildrenSkipCount(child, i); |
| 843 |
continue; |
| 844 |
} |
| 845 |
|
| 846 |
nonSkippedChildCount++; |
| 847 |
if (hasDividerBeforeChildAt(i)) { |
| 848 |
mTotalLength += mDividerHeight; |
| 849 |
} |
| 850 |
|
| 851 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 852 |
|
| 853 |
totalWeight += lp.weight; |
| 854 |
|
| 855 |
final boolean useExcessSpace = lp.height == 0 && lp.weight > 0; |
| 856 |
if (heightMode == MeasureSpec.EXACTLY && useExcessSpace) { |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
final int totalLength = mTotalLength; |
| 861 |
mTotalLength = Math.max(totalLength, totalLength + lp.topMargin + lp.bottomMargin); |
| 862 |
skippedMeasure = true; |
| 863 |
} else { |
| 864 |
if (useExcessSpace) { |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
lp.height = LayoutParams.WRAP_CONTENT; |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
final int usedHeight = totalWeight == 0 ? mTotalLength : 0; |
| 878 |
measureChildBeforeLayout(child, i, widthMeasureSpec, 0, |
| 879 |
heightMeasureSpec, usedHeight); |
| 880 |
|
| 881 |
final int childHeight = child.getMeasuredHeight(); |
| 882 |
if (useExcessSpace) { |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
lp.height = 0; |
| 887 |
consumedExcessSpace += childHeight; |
| 888 |
} |
| 889 |
|
| 890 |
final int totalLength = mTotalLength; |
| 891 |
mTotalLength = Math.max(totalLength, totalLength + childHeight + lp.topMargin + |
| 892 |
lp.bottomMargin + getNextLocationOffset(child)); |
| 893 |
|
| 894 |
if (useLargestChild) { |
| 895 |
largestChildHeight = Math.max(childHeight, largestChildHeight); |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
if ((baselineChildIndex >= 0) && (baselineChildIndex == i + 1)) { |
| 904 |
mBaselineChildTop = mTotalLength; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
if (i < baselineChildIndex && lp.weight > 0) { |
| 911 |
throw new RuntimeException("A child of LinearLayout with index " |
| 912 |
+ "less than mBaselineAlignedChildIndex has weight > 0, which " |
| 913 |
+ "won't work. Either remove the weight, or don't set " |
| 914 |
+ "mBaselineAlignedChildIndex."); |
| 915 |
} |
| 916 |
|
| 917 |
boolean matchWidthLocally = false; |
| 918 |
if (widthMode != MeasureSpec.EXACTLY && lp.width == LayoutParams.MATCH_PARENT) { |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
matchWidth = true; |
| 924 |
matchWidthLocally = true; |
| 925 |
} |
| 926 |
|
| 927 |
final int margin = lp.leftMargin + lp.rightMargin; |
| 928 |
final int measuredWidth = child.getMeasuredWidth() + margin; |
| 929 |
maxWidth = Math.max(maxWidth, measuredWidth); |
| 930 |
childState = combineMeasuredStates(childState, child.getMeasuredState()); |
| 931 |
|
| 932 |
allFillParent = allFillParent && lp.width == LayoutParams.MATCH_PARENT; |
| 933 |
if (lp.weight > 0) { |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
weightedMaxWidth = Math.max(weightedMaxWidth, |
| 939 |
matchWidthLocally ? margin : measuredWidth); |
| 940 |
} else { |
| 941 |
alternativeMaxWidth = Math.max(alternativeMaxWidth, |
| 942 |
matchWidthLocally ? margin : measuredWidth); |
| 943 |
} |
| 944 |
|
| 945 |
i += getChildrenSkipCount(child, i); |
| 946 |
} |
| 947 |
|
| 948 |
if (nonSkippedChildCount > 0 && hasDividerBeforeChildAt(count)) { |
| 949 |
mTotalLength += mDividerHeight; |
| 950 |
} |
| 951 |
|
| 952 |
if (useLargestChild && |
| 953 |
(heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED)) { |
| 954 |
mTotalLength = 0; |
| 955 |
|
| 956 |
for (int i = 0; i < count; ++i) { |
| 957 |
final View child = getVirtualChildAt(i); |
| 958 |
if (child == null) { |
| 959 |
mTotalLength += measureNullChild(i); |
| 960 |
continue; |
| 961 |
} |
| 962 |
|
| 963 |
if (child.getVisibility() == GONE) { |
| 964 |
i += getChildrenSkipCount(child, i); |
| 965 |
continue; |
| 966 |
} |
| 967 |
|
| 968 |
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) |
| 969 |
child.getLayoutParams(); |
| 970 |
|
| 971 |
final int totalLength = mTotalLength; |
| 972 |
mTotalLength = Math.max(totalLength, totalLength + largestChildHeight + |
| 973 |
lp.topMargin + lp.bottomMargin + getNextLocationOffset(child)); |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
mTotalLength += mPaddingTop + mPaddingBottom; |
| 979 |
|
| 980 |
int heightSize = mTotalLength; |
| 981 |
|
| 982 |
|
| 983 |
heightSize = Math.max(heightSize, getSuggestedMinimumHeight()); |
| 984 |
|
| 985 |
|
| 986 |
int heightSizeAndState = resolveSizeAndState(heightSize, heightMeasureSpec, 0); |
| 987 |
heightSize = heightSizeAndState & MEASURED_SIZE_MASK; |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
int remainingExcess = heightSize - mTotalLength |
| 992 |
+ (mAllowInconsistentMeasurement ? 0 : consumedExcessSpace); |
| 993 |
if (skippedMeasure |
| 994 |
|| ((sRemeasureWeightedChildren || remainingExcess != 0) && totalWeight > 0.0f)) { |
| 995 |
float remainingWeightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight; |
| 996 |
|
| 997 |
mTotalLength = 0; |
| 998 |
|
| 999 |
for (int i = 0; i < count; ++i) { |
| 1000 |
final View child = getVirtualChildAt(i); |
| 1001 |
if (child == null || child.getVisibility() == View.GONE) { |
| 1002 |
continue; |
| 1003 |
} |
| 1004 |
|
| 1005 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1006 |
final float childWeight = lp.weight; |
| 1007 |
if (childWeight > 0) { |
| 1008 |
final int share = (int) (childWeight * remainingExcess / remainingWeightSum); |
| 1009 |
remainingExcess -= share; |
| 1010 |
remainingWeightSum -= childWeight; |
| 1011 |
|
| 1012 |
final int childHeight; |
| 1013 |
if (mUseLargestChild && heightMode != MeasureSpec.EXACTLY) { |
| 1014 |
childHeight = largestChildHeight; |
| 1015 |
} else if (lp.height == 0 && (!mAllowInconsistentMeasurement |
| 1016 |
|| heightMode == MeasureSpec.EXACTLY)) { |
| 1017 |
|
| 1018 |
|
| 1019 |
childHeight = share; |
| 1020 |
} else { |
| 1021 |
|
| 1022 |
|
| 1023 |
childHeight = child.getMeasuredHeight() + share; |
| 1024 |
} |
| 1025 |
|
| 1026 |
final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 1027 |
Math.max(0, childHeight), MeasureSpec.EXACTLY); |
| 1028 |
final int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, |
| 1029 |
mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin, |
| 1030 |
lp.width); |
| 1031 |
child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 1032 |
|
| 1033 |
|
| 1034 |
childState = combineMeasuredStates(childState, child.getMeasuredState() |
| 1035 |
& (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT)); |
| 1036 |
} |
| 1037 |
|
| 1038 |
final int margin = lp.leftMargin + lp.rightMargin; |
| 1039 |
final int measuredWidth = child.getMeasuredWidth() + margin; |
| 1040 |
maxWidth = Math.max(maxWidth, measuredWidth); |
| 1041 |
|
| 1042 |
boolean matchWidthLocally = widthMode != MeasureSpec.EXACTLY && |
| 1043 |
lp.width == LayoutParams.MATCH_PARENT; |
| 1044 |
|
| 1045 |
alternativeMaxWidth = Math.max(alternativeMaxWidth, |
| 1046 |
matchWidthLocally ? margin : measuredWidth); |
| 1047 |
|
| 1048 |
allFillParent = allFillParent && lp.width == LayoutParams.MATCH_PARENT; |
| 1049 |
|
| 1050 |
final int totalLength = mTotalLength; |
| 1051 |
mTotalLength = Math.max(totalLength, totalLength + child.getMeasuredHeight() + |
| 1052 |
lp.topMargin + lp.bottomMargin + getNextLocationOffset(child)); |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
mTotalLength += mPaddingTop + mPaddingBottom; |
| 1057 |
|
| 1058 |
} else { |
| 1059 |
alternativeMaxWidth = Math.max(alternativeMaxWidth, |
| 1060 |
weightedMaxWidth); |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
if (useLargestChild && heightMode != MeasureSpec.EXACTLY) { |
| 1066 |
for (int i = 0; i < count; i++) { |
| 1067 |
final View child = getVirtualChildAt(i); |
| 1068 |
if (child == null || child.getVisibility() == View.GONE) { |
| 1069 |
continue; |
| 1070 |
} |
| 1071 |
|
| 1072 |
final LinearLayout.LayoutParams lp = |
| 1073 |
(LinearLayout.LayoutParams) child.getLayoutParams(); |
| 1074 |
|
| 1075 |
float childExtra = lp.weight; |
| 1076 |
if (childExtra > 0) { |
| 1077 |
child.measure( |
| 1078 |
MeasureSpec.makeMeasureSpec(child.getMeasuredWidth(), |
| 1079 |
MeasureSpec.EXACTLY), |
| 1080 |
MeasureSpec.makeMeasureSpec(largestChildHeight, |
| 1081 |
MeasureSpec.EXACTLY)); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
if (!allFillParent && widthMode != MeasureSpec.EXACTLY) { |
| 1088 |
maxWidth = alternativeMaxWidth; |
| 1089 |
} |
| 1090 |
|
| 1091 |
maxWidth += mPaddingLeft + mPaddingRight; |
| 1092 |
|
| 1093 |
|
| 1094 |
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); |
| 1095 |
|
| 1096 |
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), |
| 1097 |
heightSizeAndState); |
| 1098 |
|
| 1099 |
if (matchWidth) { |
| 1100 |
forceUniformWidth(count, heightMeasureSpec); |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
private void forceUniformWidth(int count, int heightMeasureSpec) { |
| 1105 |
|
| 1106 |
int uniformMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), |
| 1107 |
MeasureSpec.EXACTLY); |
| 1108 |
for (int i = 0; i< count; ++i) { |
| 1109 |
final View child = getVirtualChildAt(i); |
| 1110 |
if (child != null && child.getVisibility() != GONE) { |
| 1111 |
LinearLayout.LayoutParams lp = ((LinearLayout.LayoutParams)child.getLayoutParams()); |
| 1112 |
|
| 1113 |
if (lp.width == LayoutParams.MATCH_PARENT) { |
| 1114 |
|
| 1115 |
|
| 1116 |
int oldHeight = lp.height; |
| 1117 |
lp.height = child.getMeasuredHeight(); |
| 1118 |
|
| 1119 |
|
| 1120 |
measureChildWithMargins(child, uniformMeasureSpec, 0, heightMeasureSpec, 0); |
| 1121 |
lp.height = oldHeight; |
| 1122 |
} |
| 1123 |
} |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) { |
| 1139 |
mTotalLength = 0; |
| 1140 |
int maxHeight = 0; |
| 1141 |
int childState = 0; |
| 1142 |
int alternativeMaxHeight = 0; |
| 1143 |
int weightedMaxHeight = 0; |
| 1144 |
boolean allFillParent = true; |
| 1145 |
float totalWeight = 0; |
| 1146 |
|
| 1147 |
final int count = getVirtualChildCount(); |
| 1148 |
|
| 1149 |
final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 1150 |
final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 1151 |
|
| 1152 |
boolean matchHeight = false; |
| 1153 |
boolean skippedMeasure = false; |
| 1154 |
|
| 1155 |
if (mMaxAscent == null || mMaxDescent == null) { |
| 1156 |
mMaxAscent = new int[VERTICAL_GRAVITY_COUNT]; |
| 1157 |
mMaxDescent = new int[VERTICAL_GRAVITY_COUNT]; |
| 1158 |
} |
| 1159 |
|
| 1160 |
final int[] maxAscent = mMaxAscent; |
| 1161 |
final int[] maxDescent = mMaxDescent; |
| 1162 |
|
| 1163 |
maxAscent[0] = maxAscent[1] = maxAscent[2] = maxAscent[3] = -1; |
| 1164 |
maxDescent[0] = maxDescent[1] = maxDescent[2] = maxDescent[3] = -1; |
| 1165 |
|
| 1166 |
final boolean baselineAligned = mBaselineAligned; |
| 1167 |
final boolean useLargestChild = mUseLargestChild; |
| 1168 |
|
| 1169 |
final boolean isExactly = widthMode == MeasureSpec.EXACTLY; |
| 1170 |
|
| 1171 |
int largestChildWidth = Integer.MIN_VALUE; |
| 1172 |
int usedExcessSpace = 0; |
| 1173 |
|
| 1174 |
int nonSkippedChildCount = 0; |
| 1175 |
|
| 1176 |
|
| 1177 |
for (int i = 0; i < count; ++i) { |
| 1178 |
final View child = getVirtualChildAt(i); |
| 1179 |
if (child == null) { |
| 1180 |
mTotalLength += measureNullChild(i); |
| 1181 |
continue; |
| 1182 |
} |
| 1183 |
|
| 1184 |
if (child.getVisibility() == GONE) { |
| 1185 |
i += getChildrenSkipCount(child, i); |
| 1186 |
continue; |
| 1187 |
} |
| 1188 |
|
| 1189 |
nonSkippedChildCount++; |
| 1190 |
if (hasDividerBeforeChildAt(i)) { |
| 1191 |
mTotalLength += mDividerWidth; |
| 1192 |
} |
| 1193 |
|
| 1194 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1195 |
|
| 1196 |
totalWeight += lp.weight; |
| 1197 |
|
| 1198 |
final boolean useExcessSpace = lp.width == 0 && lp.weight > 0; |
| 1199 |
if (widthMode == MeasureSpec.EXACTLY && useExcessSpace) { |
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
if (isExactly) { |
| 1204 |
mTotalLength += lp.leftMargin + lp.rightMargin; |
| 1205 |
} else { |
| 1206 |
final int totalLength = mTotalLength; |
| 1207 |
mTotalLength = Math.max(totalLength, totalLength + |
| 1208 |
lp.leftMargin + lp.rightMargin); |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
if (baselineAligned) { |
| 1217 |
final int freeWidthSpec = MeasureSpec.makeSafeMeasureSpec( |
| 1218 |
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.UNSPECIFIED); |
| 1219 |
final int freeHeightSpec = MeasureSpec.makeSafeMeasureSpec( |
| 1220 |
MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.UNSPECIFIED); |
| 1221 |
child.measure(freeWidthSpec, freeHeightSpec); |
| 1222 |
} else { |
| 1223 |
skippedMeasure = true; |
| 1224 |
} |
| 1225 |
} else { |
| 1226 |
if (useExcessSpace) { |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
lp.width = LayoutParams.WRAP_CONTENT; |
| 1233 |
} |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
final int usedWidth = totalWeight == 0 ? mTotalLength : 0; |
| 1240 |
measureChildBeforeLayout(child, i, widthMeasureSpec, usedWidth, |
| 1241 |
heightMeasureSpec, 0); |
| 1242 |
|
| 1243 |
final int childWidth = child.getMeasuredWidth(); |
| 1244 |
if (useExcessSpace) { |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
lp.width = 0; |
| 1249 |
usedExcessSpace += childWidth; |
| 1250 |
} |
| 1251 |
|
| 1252 |
if (isExactly) { |
| 1253 |
mTotalLength += childWidth + lp.leftMargin + lp.rightMargin |
| 1254 |
+ getNextLocationOffset(child); |
| 1255 |
} else { |
| 1256 |
final int totalLength = mTotalLength; |
| 1257 |
mTotalLength = Math.max(totalLength, totalLength + childWidth + lp.leftMargin |
| 1258 |
+ lp.rightMargin + getNextLocationOffset(child)); |
| 1259 |
} |
| 1260 |
|
| 1261 |
if (useLargestChild) { |
| 1262 |
largestChildWidth = Math.max(childWidth, largestChildWidth); |
| 1263 |
} |
| 1264 |
} |
| 1265 |
|
| 1266 |
boolean matchHeightLocally = false; |
| 1267 |
if (heightMode != MeasureSpec.EXACTLY && lp.height == LayoutParams.MATCH_PARENT) { |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
matchHeight = true; |
| 1272 |
matchHeightLocally = true; |
| 1273 |
} |
| 1274 |
|
| 1275 |
final int margin = lp.topMargin + lp.bottomMargin; |
| 1276 |
final int childHeight = child.getMeasuredHeight() + margin; |
| 1277 |
childState = combineMeasuredStates(childState, child.getMeasuredState()); |
| 1278 |
|
| 1279 |
if (baselineAligned) { |
| 1280 |
final int childBaseline = child.getBaseline(); |
| 1281 |
if (childBaseline != -1) { |
| 1282 |
|
| 1283 |
|
| 1284 |
final int gravity = (lp.gravity < 0 ? mGravity : lp.gravity) |
| 1285 |
& Gravity.VERTICAL_GRAVITY_MASK; |
| 1286 |
final int index = ((gravity >> Gravity.AXIS_Y_SHIFT) |
| 1287 |
& ~Gravity.AXIS_SPECIFIED) >> 1; |
| 1288 |
|
| 1289 |
maxAscent[index] = Math.max(maxAscent[index], childBaseline); |
| 1290 |
maxDescent[index] = Math.max(maxDescent[index], childHeight - childBaseline); |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
maxHeight = Math.max(maxHeight, childHeight); |
| 1295 |
|
| 1296 |
allFillParent = allFillParent && lp.height == LayoutParams.MATCH_PARENT; |
| 1297 |
if (lp.weight > 0) { |
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
weightedMaxHeight = Math.max(weightedMaxHeight, |
| 1303 |
matchHeightLocally ? margin : childHeight); |
| 1304 |
} else { |
| 1305 |
alternativeMaxHeight = Math.max(alternativeMaxHeight, |
| 1306 |
matchHeightLocally ? margin : childHeight); |
| 1307 |
} |
| 1308 |
|
| 1309 |
i += getChildrenSkipCount(child, i); |
| 1310 |
} |
| 1311 |
|
| 1312 |
if (nonSkippedChildCount > 0 && hasDividerBeforeChildAt(count)) { |
| 1313 |
mTotalLength += mDividerWidth; |
| 1314 |
} |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
if (maxAscent[INDEX_TOP] != -1 || |
| 1319 |
maxAscent[INDEX_CENTER_VERTICAL] != -1 || |
| 1320 |
maxAscent[INDEX_BOTTOM] != -1 || |
| 1321 |
maxAscent[INDEX_FILL] != -1) { |
| 1322 |
final int ascent = Math.max(maxAscent[INDEX_FILL], |
| 1323 |
Math.max(maxAscent[INDEX_CENTER_VERTICAL], |
| 1324 |
Math.max(maxAscent[INDEX_TOP], maxAscent[INDEX_BOTTOM]))); |
| 1325 |
final int descent = Math.max(maxDescent[INDEX_FILL], |
| 1326 |
Math.max(maxDescent[INDEX_CENTER_VERTICAL], |
| 1327 |
Math.max(maxDescent[INDEX_TOP], maxDescent[INDEX_BOTTOM]))); |
| 1328 |
maxHeight = Math.max(maxHeight, ascent + descent); |
| 1329 |
} |
| 1330 |
|
| 1331 |
if (useLargestChild && |
| 1332 |
(widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED)) { |
| 1333 |
mTotalLength = 0; |
| 1334 |
nonSkippedChildCount = 0; |
| 1335 |
|
| 1336 |
for (int i = 0; i < count; ++i) { |
| 1337 |
final View child = getVirtualChildAt(i); |
| 1338 |
if (child == null) { |
| 1339 |
mTotalLength += measureNullChild(i); |
| 1340 |
continue; |
| 1341 |
} |
| 1342 |
|
| 1343 |
if (child.getVisibility() == GONE) { |
| 1344 |
i += getChildrenSkipCount(child, i); |
| 1345 |
continue; |
| 1346 |
} |
| 1347 |
|
| 1348 |
nonSkippedChildCount++; |
| 1349 |
if (hasDividerBeforeChildAt(i)) { |
| 1350 |
mTotalLength += mDividerWidth; |
| 1351 |
} |
| 1352 |
|
| 1353 |
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) |
| 1354 |
child.getLayoutParams(); |
| 1355 |
if (isExactly) { |
| 1356 |
mTotalLength += largestChildWidth + lp.leftMargin + lp.rightMargin + |
| 1357 |
getNextLocationOffset(child); |
| 1358 |
} else { |
| 1359 |
final int totalLength = mTotalLength; |
| 1360 |
mTotalLength = Math.max(totalLength, totalLength + largestChildWidth + |
| 1361 |
lp.leftMargin + lp.rightMargin + getNextLocationOffset(child)); |
| 1362 |
} |
| 1363 |
} |
| 1364 |
|
| 1365 |
if (nonSkippedChildCount > 0 && hasDividerBeforeChildAt(count)) { |
| 1366 |
mTotalLength += mDividerWidth; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
mTotalLength += mPaddingLeft + mPaddingRight; |
| 1372 |
|
| 1373 |
int widthSize = mTotalLength; |
| 1374 |
|
| 1375 |
|
| 1376 |
widthSize = Math.max(widthSize, getSuggestedMinimumWidth()); |
| 1377 |
|
| 1378 |
|
| 1379 |
int widthSizeAndState = resolveSizeAndState(widthSize, widthMeasureSpec, 0); |
| 1380 |
widthSize = widthSizeAndState & MEASURED_SIZE_MASK; |
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
int remainingExcess = widthSize - mTotalLength |
| 1386 |
+ (mAllowInconsistentMeasurement ? 0 : usedExcessSpace); |
| 1387 |
if (skippedMeasure |
| 1388 |
|| ((sRemeasureWeightedChildren || remainingExcess != 0) && totalWeight > 0.0f)) { |
| 1389 |
float remainingWeightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight; |
| 1390 |
|
| 1391 |
maxAscent[0] = maxAscent[1] = maxAscent[2] = maxAscent[3] = -1; |
| 1392 |
maxDescent[0] = maxDescent[1] = maxDescent[2] = maxDescent[3] = -1; |
| 1393 |
maxHeight = -1; |
| 1394 |
|
| 1395 |
mTotalLength = 0; |
| 1396 |
nonSkippedChildCount = 0; |
| 1397 |
|
| 1398 |
for (int i = 0; i < count; ++i) { |
| 1399 |
final View child = getVirtualChildAt(i); |
| 1400 |
if (child == null || child.getVisibility() == View.GONE) { |
| 1401 |
continue; |
| 1402 |
} |
| 1403 |
|
| 1404 |
nonSkippedChildCount++; |
| 1405 |
if (hasDividerBeforeChildAt(i)) { |
| 1406 |
mTotalLength += mDividerWidth; |
| 1407 |
} |
| 1408 |
|
| 1409 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1410 |
final float childWeight = lp.weight; |
| 1411 |
if (childWeight > 0) { |
| 1412 |
final int share = (int) (childWeight * remainingExcess / remainingWeightSum); |
| 1413 |
remainingExcess -= share; |
| 1414 |
remainingWeightSum -= childWeight; |
| 1415 |
|
| 1416 |
final int childWidth; |
| 1417 |
if (mUseLargestChild && widthMode != MeasureSpec.EXACTLY) { |
| 1418 |
childWidth = largestChildWidth; |
| 1419 |
} else if (lp.width == 0 && (!mAllowInconsistentMeasurement |
| 1420 |
|| widthMode == MeasureSpec.EXACTLY)) { |
| 1421 |
|
| 1422 |
|
| 1423 |
childWidth = share; |
| 1424 |
} else { |
| 1425 |
|
| 1426 |
|
| 1427 |
childWidth = child.getMeasuredWidth() + share; |
| 1428 |
} |
| 1429 |
|
| 1430 |
final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 1431 |
Math.max(0, childWidth), MeasureSpec.EXACTLY); |
| 1432 |
final int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, |
| 1433 |
mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin, |
| 1434 |
lp.height); |
| 1435 |
child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 1436 |
|
| 1437 |
|
| 1438 |
childState = combineMeasuredStates(childState, |
| 1439 |
child.getMeasuredState() & MEASURED_STATE_MASK); |
| 1440 |
} |
| 1441 |
|
| 1442 |
if (isExactly) { |
| 1443 |
mTotalLength += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin + |
| 1444 |
getNextLocationOffset(child); |
| 1445 |
} else { |
| 1446 |
final int totalLength = mTotalLength; |
| 1447 |
mTotalLength = Math.max(totalLength, totalLength + child.getMeasuredWidth() + |
| 1448 |
lp.leftMargin + lp.rightMargin + getNextLocationOffset(child)); |
| 1449 |
} |
| 1450 |
|
| 1451 |
boolean matchHeightLocally = heightMode != MeasureSpec.EXACTLY && |
| 1452 |
lp.height == LayoutParams.MATCH_PARENT; |
| 1453 |
|
| 1454 |
final int margin = lp.topMargin + lp .bottomMargin; |
| 1455 |
int childHeight = child.getMeasuredHeight() + margin; |
| 1456 |
maxHeight = Math.max(maxHeight, childHeight); |
| 1457 |
alternativeMaxHeight = Math.max(alternativeMaxHeight, |
| 1458 |
matchHeightLocally ? margin : childHeight); |
| 1459 |
|
| 1460 |
allFillParent = allFillParent && lp.height == LayoutParams.MATCH_PARENT; |
| 1461 |
|
| 1462 |
if (baselineAligned) { |
| 1463 |
final int childBaseline = child.getBaseline(); |
| 1464 |
if (childBaseline != -1) { |
| 1465 |
|
| 1466 |
final int gravity = (lp.gravity < 0 ? mGravity : lp.gravity) |
| 1467 |
& Gravity.VERTICAL_GRAVITY_MASK; |
| 1468 |
final int index = ((gravity >> Gravity.AXIS_Y_SHIFT) |
| 1469 |
& ~Gravity.AXIS_SPECIFIED) >> 1; |
| 1470 |
|
| 1471 |
maxAscent[index] = Math.max(maxAscent[index], childBaseline); |
| 1472 |
maxDescent[index] = Math.max(maxDescent[index], |
| 1473 |
childHeight - childBaseline); |
| 1474 |
} |
| 1475 |
} |
| 1476 |
} |
| 1477 |
|
| 1478 |
if (nonSkippedChildCount > 0 && hasDividerBeforeChildAt(count)) { |
| 1479 |
mTotalLength += mDividerWidth; |
| 1480 |
} |
| 1481 |
|
| 1482 |
|
| 1483 |
mTotalLength += mPaddingLeft + mPaddingRight; |
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
if (maxAscent[INDEX_TOP] != -1 || |
| 1489 |
maxAscent[INDEX_CENTER_VERTICAL] != -1 || |
| 1490 |
maxAscent[INDEX_BOTTOM] != -1 || |
| 1491 |
maxAscent[INDEX_FILL] != -1) { |
| 1492 |
final int ascent = Math.max(maxAscent[INDEX_FILL], |
| 1493 |
Math.max(maxAscent[INDEX_CENTER_VERTICAL], |
| 1494 |
Math.max(maxAscent[INDEX_TOP], maxAscent[INDEX_BOTTOM]))); |
| 1495 |
final int descent = Math.max(maxDescent[INDEX_FILL], |
| 1496 |
Math.max(maxDescent[INDEX_CENTER_VERTICAL], |
| 1497 |
Math.max(maxDescent[INDEX_TOP], maxDescent[INDEX_BOTTOM]))); |
| 1498 |
maxHeight = Math.max(maxHeight, ascent + descent); |
| 1499 |
} |
| 1500 |
} else { |
| 1501 |
alternativeMaxHeight = Math.max(alternativeMaxHeight, weightedMaxHeight); |
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
if (useLargestChild && widthMode != MeasureSpec.EXACTLY) { |
| 1506 |
for (int i = 0; i < count; i++) { |
| 1507 |
final View child = getVirtualChildAt(i); |
| 1508 |
if (child == null || child.getVisibility() == View.GONE) { |
| 1509 |
continue; |
| 1510 |
} |
| 1511 |
|
| 1512 |
final LinearLayout.LayoutParams lp = |
| 1513 |
(LinearLayout.LayoutParams) child.getLayoutParams(); |
| 1514 |
|
| 1515 |
float childExtra = lp.weight; |
| 1516 |
if (childExtra > 0) { |
| 1517 |
child.measure( |
| 1518 |
MeasureSpec.makeMeasureSpec(largestChildWidth, MeasureSpec.EXACTLY), |
| 1519 |
MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(), |
| 1520 |
MeasureSpec.EXACTLY)); |
| 1521 |
} |
| 1522 |
} |
| 1523 |
} |
| 1524 |
} |
| 1525 |
|
| 1526 |
if (!allFillParent && heightMode != MeasureSpec.EXACTLY) { |
| 1527 |
maxHeight = alternativeMaxHeight; |
| 1528 |
} |
| 1529 |
|
| 1530 |
maxHeight += mPaddingTop + mPaddingBottom; |
| 1531 |
|
| 1532 |
|
| 1533 |
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); |
| 1534 |
|
| 1535 |
setMeasuredDimension(widthSizeAndState | (childState&MEASURED_STATE_MASK), |
| 1536 |
resolveSizeAndState(maxHeight, heightMeasureSpec, |
| 1537 |
(childState<<MEASURED_HEIGHT_STATE_SHIFT))); |
| 1538 |
|
| 1539 |
if (matchHeight) { |
| 1540 |
forceUniformHeight(count, widthMeasureSpec); |
| 1541 |
} |
| 1542 |
} |
| 1543 |
|
| 1544 |
private void forceUniformHeight(int count, int widthMeasureSpec) { |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
int uniformMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), |
| 1549 |
MeasureSpec.EXACTLY); |
| 1550 |
for (int i = 0; i < count; ++i) { |
| 1551 |
final View child = getVirtualChildAt(i); |
| 1552 |
if (child != null && child.getVisibility() != GONE) { |
| 1553 |
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); |
| 1554 |
|
| 1555 |
if (lp.height == LayoutParams.MATCH_PARENT) { |
| 1556 |
|
| 1557 |
|
| 1558 |
int oldWidth = lp.width; |
| 1559 |
lp.width = child.getMeasuredWidth(); |
| 1560 |
|
| 1561 |
|
| 1562 |
measureChildWithMargins(child, widthMeasureSpec, 0, uniformMeasureSpec, 0); |
| 1563 |
lp.width = oldWidth; |
| 1564 |
} |
| 1565 |
} |
| 1566 |
} |
| 1567 |
} |
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
int getChildrenSkipCount(View child, int index) { |
| 1578 |
return 0; |
| 1579 |
} |
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
int measureNullChild(int childIndex) { |
| 1589 |
return 0; |
| 1590 |
} |
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
void measureChildBeforeLayout(View child, int childIndex, |
| 1606 |
int widthMeasureSpec, int totalWidth, int heightMeasureSpec, |
| 1607 |
int totalHeight) { |
| 1608 |
measureChildWithMargins(child, widthMeasureSpec, totalWidth, |
| 1609 |
heightMeasureSpec, totalHeight); |
| 1610 |
} |
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
int getLocationOffset(View child) { |
| 1620 |
return 0; |
| 1621 |
} |
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
int getNextLocationOffset(View child) { |
| 1632 |
return 0; |
| 1633 |
} |
| 1634 |
|
| 1635 |
@Override |
| 1636 |
protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 1637 |
if (mOrientation == VERTICAL) { |
| 1638 |
layoutVertical(l, t, r, b); |
| 1639 |
} else { |
| 1640 |
layoutHorizontal(l, t, r, b); |
| 1641 |
} |
| 1642 |
} |
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
void layoutVertical(int left, int top, int right, int bottom) { |
| 1657 |
final int paddingLeft = mPaddingLeft; |
| 1658 |
|
| 1659 |
int childTop; |
| 1660 |
int childLeft; |
| 1661 |
|
| 1662 |
|
| 1663 |
final int width = right - left; |
| 1664 |
int childRight = width - mPaddingRight; |
| 1665 |
|
| 1666 |
|
| 1667 |
int childSpace = width - paddingLeft - mPaddingRight; |
| 1668 |
|
| 1669 |
final int count = getVirtualChildCount(); |
| 1670 |
|
| 1671 |
final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 1672 |
final int minorGravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
| 1673 |
|
| 1674 |
switch (majorGravity) { |
| 1675 |
case Gravity.BOTTOM: |
| 1676 |
|
| 1677 |
childTop = mPaddingTop + bottom - top - mTotalLength; |
| 1678 |
break; |
| 1679 |
|
| 1680 |
|
| 1681 |
case Gravity.CENTER_VERTICAL: |
| 1682 |
childTop = mPaddingTop + (bottom - top - mTotalLength) / 2; |
| 1683 |
break; |
| 1684 |
|
| 1685 |
case Gravity.TOP: |
| 1686 |
default: |
| 1687 |
childTop = mPaddingTop; |
| 1688 |
break; |
| 1689 |
} |
| 1690 |
|
| 1691 |
for (int i = 0; i < count; i++) { |
| 1692 |
final View child = getVirtualChildAt(i); |
| 1693 |
if (child == null) { |
| 1694 |
childTop += measureNullChild(i); |
| 1695 |
} else if (child.getVisibility() != GONE) { |
| 1696 |
final int childWidth = child.getMeasuredWidth(); |
| 1697 |
final int childHeight = child.getMeasuredHeight(); |
| 1698 |
|
| 1699 |
final LinearLayout.LayoutParams lp = |
| 1700 |
(LinearLayout.LayoutParams) child.getLayoutParams(); |
| 1701 |
|
| 1702 |
int gravity = lp.gravity; |
| 1703 |
if (gravity < 0) { |
| 1704 |
gravity = minorGravity; |
| 1705 |
} |
| 1706 |
final int layoutDirection = getLayoutDirection(); |
| 1707 |
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection); |
| 1708 |
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { |
| 1709 |
case Gravity.CENTER_HORIZONTAL: |
| 1710 |
childLeft = paddingLeft + ((childSpace - childWidth) / 2) |
| 1711 |
+ lp.leftMargin - lp.rightMargin; |
| 1712 |
break; |
| 1713 |
|
| 1714 |
case Gravity.RIGHT: |
| 1715 |
childLeft = childRight - childWidth - lp.rightMargin; |
| 1716 |
break; |
| 1717 |
|
| 1718 |
case Gravity.LEFT: |
| 1719 |
default: |
| 1720 |
childLeft = paddingLeft + lp.leftMargin; |
| 1721 |
break; |
| 1722 |
} |
| 1723 |
|
| 1724 |
if (hasDividerBeforeChildAt(i)) { |
| 1725 |
childTop += mDividerHeight; |
| 1726 |
} |
| 1727 |
|
| 1728 |
childTop += lp.topMargin; |
| 1729 |
setChildFrame(child, childLeft, childTop + getLocationOffset(child), |
| 1730 |
childWidth, childHeight); |
| 1731 |
childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child); |
| 1732 |
|
| 1733 |
i += getChildrenSkipCount(child, i); |
| 1734 |
} |
| 1735 |
} |
| 1736 |
} |
| 1737 |
|
| 1738 |
@Override |
| 1739 |
public void onRtlPropertiesChanged(@ResolvedLayoutDir int layoutDirection) { |
| 1740 |
super.onRtlPropertiesChanged(layoutDirection); |
| 1741 |
if (layoutDirection != mLayoutDirection) { |
| 1742 |
mLayoutDirection = layoutDirection; |
| 1743 |
if (mOrientation == HORIZONTAL) { |
| 1744 |
requestLayout(); |
| 1745 |
} |
| 1746 |
} |
| 1747 |
} |
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
void layoutHorizontal(int left, int top, int right, int bottom) { |
| 1762 |
final boolean isLayoutRtl = isLayoutRtl(); |
| 1763 |
final int paddingTop = mPaddingTop; |
| 1764 |
|
| 1765 |
int childTop; |
| 1766 |
int childLeft; |
| 1767 |
|
| 1768 |
|
| 1769 |
final int height = bottom - top; |
| 1770 |
int childBottom = height - mPaddingBottom; |
| 1771 |
|
| 1772 |
|
| 1773 |
int childSpace = height - paddingTop - mPaddingBottom; |
| 1774 |
|
| 1775 |
final int count = getVirtualChildCount(); |
| 1776 |
|
| 1777 |
final int majorGravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
| 1778 |
final int minorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 1779 |
|
| 1780 |
final boolean baselineAligned = mBaselineAligned; |
| 1781 |
|
| 1782 |
final int[] maxAscent = mMaxAscent; |
| 1783 |
final int[] maxDescent = mMaxDescent; |
| 1784 |
|
| 1785 |
final int layoutDirection = getLayoutDirection(); |
| 1786 |
switch (Gravity.getAbsoluteGravity(majorGravity, layoutDirection)) { |
| 1787 |
case Gravity.RIGHT: |
| 1788 |
|
| 1789 |
childLeft = mPaddingLeft + right - left - mTotalLength; |
| 1790 |
break; |
| 1791 |
|
| 1792 |
case Gravity.CENTER_HORIZONTAL: |
| 1793 |
|
| 1794 |
childLeft = mPaddingLeft + (right - left - mTotalLength) / 2; |
| 1795 |
break; |
| 1796 |
|
| 1797 |
case Gravity.LEFT: |
| 1798 |
default: |
| 1799 |
childLeft = mPaddingLeft; |
| 1800 |
break; |
| 1801 |
} |
| 1802 |
|
| 1803 |
int start = 0; |
| 1804 |
int dir = 1; |
| 1805 |
|
| 1806 |
if (isLayoutRtl) { |
| 1807 |
start = count - 1; |
| 1808 |
dir = -1; |
| 1809 |
} |
| 1810 |
|
| 1811 |
for (int i = 0; i < count; i++) { |
| 1812 |
final int childIndex = start + dir * i; |
| 1813 |
final View child = getVirtualChildAt(childIndex); |
| 1814 |
if (child == null) { |
| 1815 |
childLeft += measureNullChild(childIndex); |
| 1816 |
} else if (child.getVisibility() != GONE) { |
| 1817 |
final int childWidth = child.getMeasuredWidth(); |
| 1818 |
final int childHeight = child.getMeasuredHeight(); |
| 1819 |
int childBaseline = -1; |
| 1820 |
|
| 1821 |
final LinearLayout.LayoutParams lp = |
| 1822 |
(LinearLayout.LayoutParams) child.getLayoutParams(); |
| 1823 |
|
| 1824 |
if (baselineAligned && lp.height != LayoutParams.MATCH_PARENT) { |
| 1825 |
childBaseline = child.getBaseline(); |
| 1826 |
} |
| 1827 |
|
| 1828 |
int gravity = lp.gravity; |
| 1829 |
if (gravity < 0) { |
| 1830 |
gravity = minorGravity; |
| 1831 |
} |
| 1832 |
|
| 1833 |
switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) { |
| 1834 |
case Gravity.TOP: |
| 1835 |
childTop = paddingTop + lp.topMargin; |
| 1836 |
if (childBaseline != -1) { |
| 1837 |
childTop += maxAscent[INDEX_TOP] - childBaseline; |
| 1838 |
} |
| 1839 |
break; |
| 1840 |
|
| 1841 |
case Gravity.CENTER_VERTICAL: |
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
childTop = paddingTop + ((childSpace - childHeight) / 2) |
| 1854 |
+ lp.topMargin - lp.bottomMargin; |
| 1855 |
break; |
| 1856 |
|
| 1857 |
case Gravity.BOTTOM: |
| 1858 |
childTop = childBottom - childHeight - lp.bottomMargin; |
| 1859 |
if (childBaseline != -1) { |
| 1860 |
int descent = child.getMeasuredHeight() - childBaseline; |
| 1861 |
childTop -= (maxDescent[INDEX_BOTTOM] - descent); |
| 1862 |
} |
| 1863 |
break; |
| 1864 |
default: |
| 1865 |
childTop = paddingTop; |
| 1866 |
break; |
| 1867 |
} |
| 1868 |
|
| 1869 |
if (isLayoutRtl) { |
| 1870 |
|
| 1871 |
|
| 1872 |
if (hasDividerAfterChildAt(childIndex)) { |
| 1873 |
childLeft += mDividerWidth; |
| 1874 |
} |
| 1875 |
} else if (hasDividerBeforeChildAt(childIndex)) { |
| 1876 |
childLeft += mDividerWidth; |
| 1877 |
} |
| 1878 |
|
| 1879 |
childLeft += lp.leftMargin; |
| 1880 |
setChildFrame(child, childLeft + getLocationOffset(child), childTop, |
| 1881 |
childWidth, childHeight); |
| 1882 |
childLeft += childWidth + lp.rightMargin + |
| 1883 |
getNextLocationOffset(child); |
| 1884 |
|
| 1885 |
i += getChildrenSkipCount(child, childIndex); |
| 1886 |
} |
| 1887 |
} |
| 1888 |
} |
| 1889 |
|
| 1890 |
private void setChildFrame(View child, int left, int top, int width, int height) { |
| 1891 |
child.layout(left, top, left + width, top + height); |
| 1892 |
} |
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
public void setOrientation(@OrientationMode int orientation) { |
| 1902 |
if (mOrientation != orientation) { |
| 1903 |
mOrientation = orientation; |
| 1904 |
requestLayout(); |
| 1905 |
} |
| 1906 |
} |
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
@OrientationMode |
| 1914 |
@InspectableProperty(enumMapping = { |
| 1915 |
@InspectableProperty.EnumEntry(value = HORIZONTAL, name = "horizontal"), |
| 1916 |
@InspectableProperty.EnumEntry(value = VERTICAL, name = "vertical") |
| 1917 |
}) |
| 1918 |
public int getOrientation() { |
| 1919 |
return mOrientation; |
| 1920 |
} |
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
@android.view.RemotableViewMethod |
| 1933 |
public void setGravity(int gravity) { |
| 1934 |
if (mGravity != gravity) { |
| 1935 |
if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) { |
| 1936 |
gravity |= Gravity.START; |
| 1937 |
} |
| 1938 |
|
| 1939 |
if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) { |
| 1940 |
gravity |= Gravity.TOP; |
| 1941 |
} |
| 1942 |
|
| 1943 |
mGravity = gravity; |
| 1944 |
requestLayout(); |
| 1945 |
} |
| 1946 |
} |
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
@InspectableProperty(valueType = InspectableProperty.ValueType.GRAVITY) |
| 1955 |
public int getGravity() { |
| 1956 |
return mGravity; |
| 1957 |
} |
| 1958 |
|
| 1959 |
@android.view.RemotableViewMethod |
| 1960 |
public void setHorizontalGravity(int horizontalGravity) { |
| 1961 |
final int gravity = horizontalGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
| 1962 |
if ((mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) != gravity) { |
| 1963 |
mGravity = (mGravity & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) | gravity; |
| 1964 |
requestLayout(); |
| 1965 |
} |
| 1966 |
} |
| 1967 |
|
| 1968 |
@android.view.RemotableViewMethod |
| 1969 |
public void setVerticalGravity(int verticalGravity) { |
| 1970 |
final int gravity = verticalGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 1971 |
if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != gravity) { |
| 1972 |
mGravity = (mGravity & ~Gravity.VERTICAL_GRAVITY_MASK) | gravity; |
| 1973 |
requestLayout(); |
| 1974 |
} |
| 1975 |
} |
| 1976 |
|
| 1977 |
@Override |
| 1978 |
public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 1979 |
return new LinearLayout.LayoutParams(getContext(), attrs); |
| 1980 |
} |
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
@Override |
| 1991 |
protected LayoutParams generateDefaultLayoutParams() { |
| 1992 |
if (mOrientation == HORIZONTAL) { |
| 1993 |
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 1994 |
} else if (mOrientation == VERTICAL) { |
| 1995 |
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
| 1996 |
} |
| 1997 |
return null; |
| 1998 |
} |
| 1999 |
|
| 2000 |
@Override |
| 2001 |
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { |
| 2002 |
if (sPreserveMarginParamsInLayoutParamConversion) { |
| 2003 |
if (lp instanceof LayoutParams) { |
| 2004 |
return new LayoutParams((LayoutParams) lp); |
| 2005 |
} else if (lp instanceof MarginLayoutParams) { |
| 2006 |
return new LayoutParams((MarginLayoutParams) lp); |
| 2007 |
} |
| 2008 |
} |
| 2009 |
return new LayoutParams(lp); |
| 2010 |
} |
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
@Override |
| 2015 |
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 2016 |
return p instanceof LinearLayout.LayoutParams; |
| 2017 |
} |
| 2018 |
|
| 2019 |
@Override |
| 2020 |
public CharSequence getAccessibilityClassName() { |
| 2021 |
return LinearLayout.class.getName(); |
| 2022 |
} |
| 2023 |
|
| 2024 |
|
| 2025 |
@Override |
| 2026 |
protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 2027 |
super.encodeProperties(encoder); |
| 2028 |
encoder.addProperty("layout:baselineAligned", mBaselineAligned); |
| 2029 |
encoder.addProperty("layout:baselineAlignedChildIndex", mBaselineAlignedChildIndex); |
| 2030 |
encoder.addProperty("measurement:baselineChildTop", mBaselineChildTop); |
| 2031 |
encoder.addProperty("measurement:orientation", mOrientation); |
| 2032 |
encoder.addProperty("measurement:gravity", mGravity); |
| 2033 |
encoder.addProperty("measurement:totalLength", mTotalLength); |
| 2034 |
encoder.addProperty("layout:totalLength", mTotalLength); |
| 2035 |
encoder.addProperty("layout:useLargestChild", mUseLargestChild); |
| 2036 |
} |
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
@ViewDebug.ExportedProperty(category = "layout") |
| 2052 |
@InspectableProperty(name = "layout_weight") |
| 2053 |
public float weight; |
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
@ViewDebug.ExportedProperty(category = "layout", mapping = { |
| 2061 |
@ViewDebug.IntToString(from = -1, to = "NONE"), |
| 2062 |
@ViewDebug.IntToString(from = Gravity.NO_GRAVITY, to = "NONE"), |
| 2063 |
@ViewDebug.IntToString(from = Gravity.TOP, to = "TOP"), |
| 2064 |
@ViewDebug.IntToString(from = Gravity.BOTTOM, to = "BOTTOM"), |
| 2065 |
@ViewDebug.IntToString(from = Gravity.LEFT, to = "LEFT"), |
| 2066 |
@ViewDebug.IntToString(from = Gravity.RIGHT, to = "RIGHT"), |
| 2067 |
@ViewDebug.IntToString(from = Gravity.START, to = "START"), |
| 2068 |
@ViewDebug.IntToString(from = Gravity.END, to = "END"), |
| 2069 |
@ViewDebug.IntToString(from = Gravity.CENTER_VERTICAL, to = "CENTER_VERTICAL"), |
| 2070 |
@ViewDebug.IntToString(from = Gravity.FILL_VERTICAL, to = "FILL_VERTICAL"), |
| 2071 |
@ViewDebug.IntToString(from = Gravity.CENTER_HORIZONTAL, to = "CENTER_HORIZONTAL"), |
| 2072 |
@ViewDebug.IntToString(from = Gravity.FILL_HORIZONTAL, to = "FILL_HORIZONTAL"), |
| 2073 |
@ViewDebug.IntToString(from = Gravity.CENTER, to = "CENTER"), |
| 2074 |
@ViewDebug.IntToString(from = Gravity.FILL, to = "FILL") |
| 2075 |
}) |
| 2076 |
@InspectableProperty( |
| 2077 |
name = "layout_gravity", |
| 2078 |
valueType = InspectableProperty.ValueType.GRAVITY) |
| 2079 |
public int gravity = -1; |
| 2080 |
|
| 2081 |
|
| 2082 |
|
| 2083 |
|
| 2084 |
public LayoutParams(Context c, AttributeSet attrs) { |
| 2085 |
super(c, attrs); |
| 2086 |
TypedArray a = |
| 2087 |
c.obtainStyledAttributes(attrs, com.android.internal.R.styleable.LinearLayout_Layout); |
| 2088 |
|
| 2089 |
weight = a.getFloat(com.android.internal.R.styleable.LinearLayout_Layout_layout_weight, 0); |
| 2090 |
gravity = a.getInt(com.android.internal.R.styleable.LinearLayout_Layout_layout_gravity, -1); |
| 2091 |
|
| 2092 |
a.recycle(); |
| 2093 |
} |
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
public LayoutParams(int width, int height) { |
| 2099 |
super(width, height); |
| 2100 |
weight = 0; |
| 2101 |
} |
| 2102 |
|
| 2103 |
|
| 2104 |
|
| 2105 |
|
| 2106 |
|
| 2107 |
|
| 2108 |
|
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
public LayoutParams(int width, int height, float weight) { |
| 2114 |
super(width, height); |
| 2115 |
this.weight = weight; |
| 2116 |
} |
| 2117 |
|
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
public LayoutParams(ViewGroup.LayoutParams p) { |
| 2122 |
super(p); |
| 2123 |
} |
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
public LayoutParams(ViewGroup.MarginLayoutParams source) { |
| 2129 |
super(source); |
| 2130 |
} |
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
|
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
public LayoutParams(LayoutParams source) { |
| 2139 |
super(source); |
| 2140 |
|
| 2141 |
this.weight = source.weight; |
| 2142 |
this.gravity = source.gravity; |
| 2143 |
} |
| 2144 |
|
| 2145 |
@Override |
| 2146 |
public String debug(String output) { |
| 2147 |
return output + "LinearLayout.LayoutParams={width=" + sizeToString(width) + |
| 2148 |
", height=" + sizeToString(height) + " weight=" + weight + "}"; |
| 2149 |
} |
| 2150 |
|
| 2151 |
|
| 2152 |
@Override |
| 2153 |
@UnsupportedAppUsage |
| 2154 |
protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 2155 |
super.encodeProperties(encoder); |
| 2156 |
|
| 2157 |
encoder.addProperty("layout:weight", weight); |
| 2158 |
encoder.addProperty("layout:gravity", gravity); |
| 2159 |
} |
| 2160 |
} |
| 2161 |
} |