LinearLayout.java revision d24b8183b93e781080b2c16c487e60d51c12da31
1/* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.widget; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.util.AttributeSet; 22import android.view.Gravity; 23import android.view.View; 24import android.view.ViewDebug; 25import android.view.ViewGroup; 26import android.widget.RemoteViews.RemoteView; 27 28import com.android.internal.R; 29 30 31/** 32 * A Layout that arranges its children in a single column or a single row. The direction of 33 * the row can be set by calling {@link #setOrientation(int) setOrientation()}. 34 * You can also specify gravity, which specifies the alignment of all the child elements by 35 * calling {@link #setGravity(int) setGravity()} or specify that specific children 36 * grow to fill up any remaining space in the layout by setting the <em>weight</em> member of 37 * {@link android.widget.LinearLayout.LayoutParams LinearLayout.LayoutParams}. 38 * The default orientation is horizontal. 39 * 40 * <p> 41 * Also see {@link LinearLayout.LayoutParams android.widget.LinearLayout.LayoutParams} 42 * for layout attributes </p> 43 */ 44@RemoteView 45public class LinearLayout extends ViewGroup { 46 public static final int HORIZONTAL = 0; 47 public static final int VERTICAL = 1; 48 49 /** 50 * Whether the children of this layout are baseline aligned. Only applicable 51 * if {@link #mOrientation} is horizontal. 52 */ 53 private boolean mBaselineAligned = true; 54 55 /** 56 * If this layout is part of another layout that is baseline aligned, 57 * use the child at this index as the baseline. 58 * 59 * Note: this is orthogonal to {@link #mBaselineAligned}, which is concerned 60 * with whether the children of this layout are baseline aligned. 61 */ 62 private int mBaselineAlignedChildIndex = 0; 63 64 /** 65 * The additional offset to the child's baseline. 66 * We'll calculate the baseline of this layout as we measure vertically; for 67 * horizontal linear layouts, the offset of 0 is appropriate. 68 */ 69 private int mBaselineChildTop = 0; 70 71 private int mOrientation; 72 private int mGravity = Gravity.LEFT | Gravity.TOP; 73 private int mTotalLength; 74 75 private float mWeightSum; 76 77 private int[] mMaxAscent; 78 private int[] mMaxDescent; 79 80 private static final int VERTICAL_GRAVITY_COUNT = 4; 81 82 private static final int INDEX_CENTER_VERTICAL = 0; 83 private static final int INDEX_TOP = 1; 84 private static final int INDEX_BOTTOM = 2; 85 private static final int INDEX_FILL = 3; 86 87 public LinearLayout(Context context) { 88 super(context); 89 } 90 91 public LinearLayout(Context context, AttributeSet attrs) { 92 super(context, attrs); 93 94 TypedArray a = 95 context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.LinearLayout); 96 97 int index = a.getInt(com.android.internal.R.styleable.LinearLayout_orientation, -1); 98 if (index >= 0) { 99 setOrientation(index); 100 } 101 102 index = a.getInt(com.android.internal.R.styleable.LinearLayout_gravity, -1); 103 if (index >= 0) { 104 setGravity(index); 105 } 106 107 boolean baselineAligned = a.getBoolean(R.styleable.LinearLayout_baselineAligned, true); 108 if (!baselineAligned) { 109 setBaselineAligned(baselineAligned); 110 } 111 112 mWeightSum = a.getFloat(R.styleable.LinearLayout_weightSum, -1.0f); 113 114 mBaselineAlignedChildIndex = 115 a.getInt(com.android.internal.R.styleable.LinearLayout_baselineAlignedChildIndex, -1); 116 117 a.recycle(); 118 } 119 120 /** 121 * <p>Indicates whether widgets contained within this layout are aligned 122 * on their baseline or not.</p> 123 * 124 * @return true when widgets are baseline-aligned, false otherwise 125 */ 126 public boolean isBaselineAligned() { 127 return mBaselineAligned; 128 } 129 130 /** 131 * <p>Defines whether widgets contained in this layout are 132 * baseline-aligned or not.</p> 133 * 134 * @param baselineAligned true to align widgets on their baseline, 135 * false otherwise 136 * 137 * @attr ref android.R.styleable#LinearLayout_baselineAligned 138 */ 139 public void setBaselineAligned(boolean baselineAligned) { 140 mBaselineAligned = baselineAligned; 141 } 142 143 @Override 144 public int getBaseline() { 145 if (mBaselineAlignedChildIndex < 0) { 146 return super.getBaseline(); 147 } 148 149 if (getChildCount() <= mBaselineAlignedChildIndex) { 150 throw new RuntimeException("mBaselineAlignedChildIndex of LinearLayout " 151 + "set to an index that is out of bounds."); 152 } 153 154 final View child = getChildAt(mBaselineAlignedChildIndex); 155 final int childBaseline = child.getBaseline(); 156 157 if (childBaseline == -1) { 158 if (mBaselineAlignedChildIndex == 0) { 159 // this is just the default case, safe to return -1 160 return -1; 161 } 162 // the user picked an index that points to something that doesn't 163 // know how to calculate its baseline. 164 throw new RuntimeException("mBaselineAlignedChildIndex of LinearLayout " 165 + "points to a View that doesn't know how to get its baseline."); 166 } 167 168 // TODO: This should try to take into account the virtual offsets 169 // (See getNextLocationOffset and getLocationOffset) 170 // We should add to childTop: 171 // sum([getNextLocationOffset(getChildAt(i)) / i < mBaselineAlignedChildIndex]) 172 // and also add: 173 // getLocationOffset(child) 174 int childTop = mBaselineChildTop; 175 176 if (mOrientation == VERTICAL) { 177 final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; 178 if (majorGravity != Gravity.TOP) { 179 switch (majorGravity) { 180 case Gravity.BOTTOM: 181 childTop = mBottom - mTop - mPaddingBottom - mTotalLength; 182 break; 183 184 case Gravity.CENTER_VERTICAL: 185 childTop += ((mBottom - mTop - mPaddingTop - mPaddingBottom) - 186 mTotalLength) / 2; 187 break; 188 } 189 } 190 } 191 192 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); 193 return childTop + lp.topMargin + childBaseline; 194 } 195 196 /** 197 * @return The index of the child that will be used if this layout is 198 * part of a larger layout that is baseline aligned, or -1 if none has 199 * been set. 200 */ 201 public int getBaselineAlignedChildIndex() { 202 return mBaselineAlignedChildIndex; 203 } 204 205 /** 206 * @param i The index of the child that will be used if this layout is 207 * part of a larger layout that is baseline aligned. 208 * 209 * @attr ref android.R.styleable#LinearLayout_baselineAlignedChildIndex 210 */ 211 public void setBaselineAlignedChildIndex(int i) { 212 if ((i < 0) || (i >= getChildCount())) { 213 throw new IllegalArgumentException("base aligned child index out " 214 + "of range (0, " + getChildCount() + ")"); 215 } 216 mBaselineAlignedChildIndex = i; 217 } 218 219 /** 220 * <p>Returns the view at the specified index. This method can be overriden 221 * to take into account virtual children. Refer to 222 * {@link android.widget.TableLayout} and {@link android.widget.TableRow} 223 * for an example.</p> 224 * 225 * @param index the child's index 226 * @return the child at the specified index 227 */ 228 View getVirtualChildAt(int index) { 229 return getChildAt(index); 230 } 231 232 /** 233 * <p>Returns the virtual number of children. This number might be different 234 * than the actual number of children if the layout can hold virtual 235 * children. Refer to 236 * {@link android.widget.TableLayout} and {@link android.widget.TableRow} 237 * for an example.</p> 238 * 239 * @return the virtual number of children 240 */ 241 int getVirtualChildCount() { 242 return getChildCount(); 243 } 244 245 /** 246 * Returns the desired weights sum. 247 * 248 * @return A number greater than 0.0f if the weight sum is defined, or 249 * a number lower than or equals to 0.0f if not weight sum is 250 * to be used. 251 */ 252 public float getWeightSum() { 253 return mWeightSum; 254 } 255 256 /** 257 * Defines the desired weights sum. If unspecified the weights sum is computed 258 * at layout time by adding the layout_weight of each child. 259 * 260 * This can be used for instance to give a single child 50% of the total 261 * available space by giving it a layout_weight of 0.5 and setting the 262 * weightSum to 1.0. 263 * 264 * @param weightSum a number greater than 0.0f, or a number lower than or equals 265 * to 0.0f if the weight sum should be computed from the children's 266 * layout_weight 267 */ 268 public void setWeightSum(float weightSum) { 269 mWeightSum = Math.max(0.0f, weightSum); 270 } 271 272 @Override 273 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 274 if (mOrientation == VERTICAL) { 275 measureVertical(widthMeasureSpec, heightMeasureSpec); 276 } else { 277 measureHorizontal(widthMeasureSpec, heightMeasureSpec); 278 } 279 } 280 281 /** 282 * Measures the children when the orientation of this LinearLayout is set 283 * to {@link #VERTICAL}. 284 * 285 * @param widthMeasureSpec Horizontal space requirements as imposed by the parent. 286 * @param heightMeasureSpec Vertical space requirements as imposed by the parent. 287 * 288 * @see #getOrientation() 289 * @see #setOrientation(int) 290 * @see #onMeasure(int, int) 291 */ 292 void measureVertical(int widthMeasureSpec, int heightMeasureSpec) { 293 mTotalLength = 0; 294 int maxWidth = 0; 295 int alternativeMaxWidth = 0; 296 int weightedMaxWidth = 0; 297 boolean allFillParent = true; 298 float totalWeight = 0; 299 300 final int count = getVirtualChildCount(); 301 302 final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 303 final int heightMode = MeasureSpec.getMode(heightMeasureSpec); 304 305 boolean matchWidth = false; 306 307 final int baselineChildIndex = mBaselineAlignedChildIndex; 308 309 // See how tall everyone is. Also remember max width. 310 for (int i = 0; i < count; ++i) { 311 final View child = getVirtualChildAt(i); 312 313 if (child == null) { 314 mTotalLength += measureNullChild(i); 315 continue; 316 } 317 318 if (child.getVisibility() == View.GONE) { 319 i += getChildrenSkipCount(child, i); 320 continue; 321 } 322 323 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); 324 325 totalWeight += lp.weight; 326 327 if (heightMode == MeasureSpec.EXACTLY && lp.height == 0 && lp.weight > 0) { 328 // Optimization: don't bother measuring children who are going to use 329 // leftover space. These views will get measured again down below if 330 // there is any leftover space. 331 mTotalLength += lp.topMargin + lp.bottomMargin; 332 } else { 333 int oldHeight = Integer.MIN_VALUE; 334 335 if (lp.height == 0 && lp.weight > 0) { 336 // heightMode is either UNSPECIFIED OR AT_MOST, and this child 337 // wanted to stretch to fill available space. Translate that to 338 // WRAP_CONTENT so that it does not end up with a height of 0 339 oldHeight = 0; 340 lp.height = LayoutParams.WRAP_CONTENT; 341 } 342 343 // Determine how big this child would like to. If this or 344 // previous children have given a weight, then we allow it to 345 // use all available space (and we will shrink things later 346 // if needed). 347 measureChildBeforeLayout( 348 child, i, widthMeasureSpec, 0, heightMeasureSpec, 349 totalWeight == 0 ? mTotalLength : 0); 350 351 if (oldHeight != Integer.MIN_VALUE) { 352 lp.height = oldHeight; 353 } 354 355 mTotalLength += child.getMeasuredHeight() + lp.topMargin + 356 lp.bottomMargin + getNextLocationOffset(child); 357 } 358 359 /** 360 * If applicable, compute the additional offset to the child's baseline 361 * we'll need later when asked {@link #getBaseline}. 362 */ 363 if ((baselineChildIndex >= 0) && (baselineChildIndex == i + 1)) { 364 mBaselineChildTop = mTotalLength; 365 } 366 367 // if we are trying to use a child index for our baseline, the above 368 // book keeping only works if there are no children above it with 369 // weight. fail fast to aid the developer. 370 if (i < baselineChildIndex && lp.weight > 0) { 371 throw new RuntimeException("A child of LinearLayout with index " 372 + "less than mBaselineAlignedChildIndex has weight > 0, which " 373 + "won't work. Either remove the weight, or don't set " 374 + "mBaselineAlignedChildIndex."); 375 } 376 377 boolean matchWidthLocally = false; 378 if (widthMode != MeasureSpec.EXACTLY && lp.width == LayoutParams.FILL_PARENT) { 379 // The width of the linear layout will scale, and at least one 380 // child said it wanted to match our width. Set a flag 381 // indicating that we need to remeasure at least that view when 382 // we know our width. 383 matchWidth = true; 384 matchWidthLocally = true; 385 } 386 387 final int margin = lp.leftMargin + lp.rightMargin; 388 final int measuredWidth = child.getMeasuredWidth() + margin; 389 maxWidth = Math.max(maxWidth, measuredWidth); 390 391 allFillParent = allFillParent && lp.width == LayoutParams.FILL_PARENT; 392 if (lp.weight > 0) { 393 /* 394 * Widths of weighted Views are bogus if we end up 395 * remeasuring, so keep them separate. 396 */ 397 weightedMaxWidth = Math.max(weightedMaxWidth, 398 matchWidthLocally ? margin : measuredWidth); 399 } else { 400 alternativeMaxWidth = Math.max(alternativeMaxWidth, 401 matchWidthLocally ? margin : measuredWidth); 402 } 403 404 i += getChildrenSkipCount(child, i); 405 } 406 407 // Add in our padding 408 mTotalLength += mPaddingTop + mPaddingBottom; 409 410 int heightSize = mTotalLength; 411 412 // Check against our minimum height 413 heightSize = Math.max(heightSize, getSuggestedMinimumHeight()); 414 415 // Reconcile our calculated size with the heightMeasureSpec 416 heightSize = resolveSize(heightSize, heightMeasureSpec); 417 418 // Either expand children with weight to take up available space or 419 // shrink them if they extend beyond our current bounds 420 int delta = heightSize - mTotalLength; 421 if (delta != 0 && totalWeight > 0.0f) { 422 float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight; 423 424 mTotalLength = 0; 425 426 for (int i = 0; i < count; ++i) { 427 final View child = getVirtualChildAt(i); 428 429 if (child.getVisibility() == View.GONE) { 430 continue; 431 } 432 433 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); 434 435 float childExtra = lp.weight; 436 if (childExtra > 0) { 437 // Child said it could absorb extra space -- give him his share 438 int share = (int) (childExtra * delta / weightSum); 439 weightSum -= childExtra; 440 delta -= share; 441 442 final int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 443 mPaddingLeft + mPaddingRight + 444 lp.leftMargin + lp.rightMargin, lp.width); 445 446 // TODO: Use a field like lp.isMeasured to figure out if this 447 // child has been previously measured 448 if ((lp.height != 0) || (heightMode != MeasureSpec.EXACTLY)) { 449 // child was measured once already above... 450 // base new measurement on stored values 451 int childHeight = child.getMeasuredHeight() + share; 452 if (childHeight < 0) { 453 childHeight = 0; 454 } 455 456 child.measure(childWidthMeasureSpec, 457 MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY)); 458 } else { 459 // child was skipped in the loop above. 460 // Measure for this first time here 461 child.measure(childWidthMeasureSpec, 462 MeasureSpec.makeMeasureSpec(share > 0 ? share : 0, 463 MeasureSpec.EXACTLY)); 464 } 465 } 466 467 final int margin = lp.leftMargin + lp.rightMargin; 468 final int measuredWidth = child.getMeasuredWidth() + margin; 469 maxWidth = Math.max(maxWidth, measuredWidth); 470 471 boolean matchWidthLocally = widthMode != MeasureSpec.EXACTLY && 472 lp.width == LayoutParams.FILL_PARENT; 473 474 alternativeMaxWidth = Math.max(alternativeMaxWidth, 475 matchWidthLocally ? margin : measuredWidth); 476 477 allFillParent = allFillParent && lp.width == LayoutParams.FILL_PARENT; 478 479 mTotalLength += child.getMeasuredHeight() + lp.topMargin + 480 lp.bottomMargin + getNextLocationOffset(child); 481 } 482 483 // Add in our padding 484 mTotalLength += mPaddingTop + mPaddingBottom; 485 } else { 486 alternativeMaxWidth = Math.max(alternativeMaxWidth, 487 weightedMaxWidth); 488 } 489 490 if (!allFillParent && widthMode != MeasureSpec.EXACTLY) { 491 maxWidth = alternativeMaxWidth; 492 } 493 494 maxWidth += mPaddingLeft + mPaddingRight; 495 496 // Check against our minimum width 497 maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); 498 499 setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec), heightSize); 500 501 if (matchWidth) { 502 forceUniformWidth(count, heightMeasureSpec); 503 } 504 } 505 506 private void forceUniformWidth(int count, int heightMeasureSpec) { 507 // Pretend that the linear layout has an exact size. 508 int uniformMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), 509 MeasureSpec.EXACTLY); 510 for (int i = 0; i< count; ++i) { 511 final View child = getVirtualChildAt(i); 512 if (child.getVisibility() != GONE) { 513 LinearLayout.LayoutParams lp = ((LinearLayout.LayoutParams)child.getLayoutParams()); 514 515 if (lp.width == LayoutParams.FILL_PARENT) { 516 // Temporarily force children to reuse their old measured height 517 // FIXME: this may not be right for something like wrapping text? 518 int oldHeight = lp.height; 519 lp.height = child.getMeasuredHeight(); 520 521 // Remeasue with new dimensions 522 measureChildWithMargins(child, uniformMeasureSpec, 0, heightMeasureSpec, 0); 523 lp.height = oldHeight; 524 } 525 } 526 } 527 } 528 529 /** 530 * Measures the children when the orientation of this LinearLayout is set 531 * to {@link #HORIZONTAL}. 532 * 533 * @param widthMeasureSpec Horizontal space requirements as imposed by the parent. 534 * @param heightMeasureSpec Vertical space requirements as imposed by the parent. 535 * 536 * @see #getOrientation() 537 * @see #setOrientation(int) 538 * @see #onMeasure(int, int) 539 */ 540 void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) { 541 mTotalLength = 0; 542 int maxHeight = 0; 543 int alternativeMaxHeight = 0; 544 int weightedMaxHeight = 0; 545 boolean allFillParent = true; 546 float totalWeight = 0; 547 548 final int count = getVirtualChildCount(); 549 550 final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 551 final int heightMode = MeasureSpec.getMode(heightMeasureSpec); 552 553 boolean matchHeight = false; 554 555 if (mMaxAscent == null || mMaxDescent == null) { 556 mMaxAscent = new int[VERTICAL_GRAVITY_COUNT]; 557 mMaxDescent = new int[VERTICAL_GRAVITY_COUNT]; 558 } 559 560 final int[] maxAscent = mMaxAscent; 561 final int[] maxDescent = mMaxDescent; 562 563 maxAscent[0] = maxAscent[1] = maxAscent[2] = maxAscent[3] = -1; 564 maxDescent[0] = maxDescent[1] = maxDescent[2] = maxDescent[3] = -1; 565 566 final boolean baselineAligned = mBaselineAligned; 567 568 // See how wide everyone is. Also remember max height. 569 for (int i = 0; i < count; ++i) { 570 final View child = getVirtualChildAt(i); 571 572 if (child == null) { 573 mTotalLength += measureNullChild(i); 574 continue; 575 } 576 577 if (child.getVisibility() == GONE) { 578 i += getChildrenSkipCount(child, i); 579 continue; 580 } 581 582 final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); 583 584 totalWeight += lp.weight; 585 586 if (widthMode == MeasureSpec.EXACTLY && lp.width == 0 && lp.weight > 0) { 587 // Optimization: don't bother measuring children who are going to use 588 // leftover space. These views will get measured again down below if 589 // there is any leftover space. 590 mTotalLength += lp.leftMargin + lp.rightMargin; 591 592 // Baseline alignment requires to measure widgets to obtain the 593 // baseline offset (in particular for TextViews). 594 // The following defeats the optimization mentioned above. 595 // Allow the child to use as much space as it wants because we 596 // can shrink things later (and re-measure). 597 if (baselineAligned) { 598 final int freeSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 599 child.measure(freeSpec, freeSpec); 600 } 601 } else { 602 int oldWidth = Integer.MIN_VALUE; 603 604 if (lp.width == 0 && lp.weight > 0) { 605 // widthMode is either UNSPECIFIED OR AT_MOST, and this child 606 // wanted to stretch to fill available space. Translate that to 607 // WRAP_CONTENT so that it does not end up with a width of 0 608 oldWidth = 0; 609 lp.width = LayoutParams.WRAP_CONTENT; 610 } 611 612 // Determine how big this child would like to be. If this or 613 // previous children have given a weight, then we allow it to 614 // use all available space (and we will shrink things later 615 // if needed). 616 measureChildBeforeLayout(child, i, widthMeasureSpec, 617 totalWeight == 0 ? mTotalLength : 0, 618 heightMeasureSpec, 0); 619 620 if (oldWidth != Integer.MIN_VALUE) { 621 lp.width = oldWidth; 622 } 623 624 mTotalLength += child.getMeasuredWidth() + lp.leftMargin + 625 lp.rightMargin + getNextLocationOffset(child); 626 } 627 628 boolean matchHeightLocally = false; 629 if (heightMode != MeasureSpec.EXACTLY && lp.height == LayoutParams.FILL_PARENT) { 630 // The height of the linear layout will scale, and at least one 631 // child said it wanted to match our height. Set a flag indicating that 632 // we need to remeasure at least that view when we know our height. 633 matchHeight = true; 634 matchHeightLocally = true; 635 } 636 637 final int margin = lp.topMargin + lp.bottomMargin; 638 final int childHeight = child.getMeasuredHeight() + margin; 639 640 if (baselineAligned) { 641 final int childBaseline = child.getBaseline(); 642 if (childBaseline != -1) { 643 // Translates the child's vertical gravity into an index 644 // in the range 0..VERTICAL_GRAVITY_COUNT 645 final int gravity = (lp.gravity < 0 ? mGravity : lp.gravity) 646 & Gravity.VERTICAL_GRAVITY_MASK; 647 final int index = ((gravity >> Gravity.AXIS_Y_SHIFT) 648 & ~Gravity.AXIS_SPECIFIED) >> 1; 649 650 maxAscent[index] = Math.max(maxAscent[index], childBaseline); 651 maxDescent[index] = Math.max(maxDescent[index], childHeight - childBaseline); 652 } 653 } 654 655 maxHeight = Math.max(maxHeight, childHeight); 656 657 allFillParent = allFillParent && lp.height == LayoutParams.FILL_PARENT; 658 if (lp.weight > 0) { 659 /* 660 * Heights of weighted Views are bogus if we end up 661 * remeasuring, so keep them separate. 662 */ 663 weightedMaxHeight = Math.max(weightedMaxHeight, 664 matchHeightLocally ? margin : childHeight); 665 } else { 666 alternativeMaxHeight = Math.max(alternativeMaxHeight, 667 matchHeightLocally ? margin : childHeight); 668 } 669 670 i += getChildrenSkipCount(child, i); 671 } 672 673 // Check mMaxAscent[INDEX_TOP] first because it maps to Gravity.TOP, 674 // the most common case 675 if (maxAscent[INDEX_TOP] != -1 || 676 maxAscent[INDEX_CENTER_VERTICAL] != -1 || 677 maxAscent[INDEX_BOTTOM] != -1 || 678 maxAscent[INDEX_FILL] != -1) { 679 final int ascent = Math.max(maxAscent[INDEX_FILL], 680 Math.max(maxAscent[INDEX_CENTER_VERTICAL], 681 Math.max(maxAscent[INDEX_TOP], maxAscent[INDEX_BOTTOM]))); 682 final int descent = Math.max(maxDescent[INDEX_FILL], 683 Math.max(maxDescent[INDEX_CENTER_VERTICAL], 684 Math.max(maxDescent[INDEX_TOP], maxDescent[INDEX_BOTTOM]))); 685 maxHeight = Math.max(maxHeight, ascent + descent); 686 } 687 688 // Add in our padding 689 mTotalLength += mPaddingLeft + mPaddingRight; 690 691 int widthSize = mTotalLength; 692 693 // Check against our minimum width 694 widthSize = Math.max(widthSize, getSuggestedMinimumWidth()); 695 696 // Reconcile our calculated size with the widthMeasureSpec 697 widthSize = resolveSize(widthSize, widthMeasureSpec); 698 699 // Either expand children with weight to take up available space or 700 // shrink them if they extend beyond our current bounds 701 int delta = widthSize - mTotalLength; 702 if (delta != 0 && totalWeight > 0.0f) { 703 float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight; 704 705 maxAscent[0] = maxAscent[1] = maxAscent[2] = maxAscent[3] = -1; 706 maxDescent[0] = maxDescent[1] = maxDescent[2] = maxDescent[3] = -1; 707 maxHeight = -1; 708 709 mTotalLength = 0; 710 711 for (int i = 0; i < count; ++i) { 712 final View child = getVirtualChildAt(i); 713 714 if (child == null || child.getVisibility() == View.GONE) { 715 continue; 716 } 717 718 final LinearLayout.LayoutParams lp = 719 (LinearLayout.LayoutParams) child.getLayoutParams(); 720 721 float childExtra = lp.weight; 722 if (childExtra > 0) { 723 // Child said it could absorb extra space -- give him his share 724 int share = (int) (childExtra * delta / weightSum); 725 weightSum -= childExtra; 726 delta -= share; 727 728 final int childHeightMeasureSpec = getChildMeasureSpec( 729 heightMeasureSpec, 730 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin, 731 lp.height); 732 733 // TODO: Use a field like lp.isMeasured to figure out if this 734 // child has been previously measured 735 if ((lp.width != 0) || (widthMode != MeasureSpec.EXACTLY)) { 736 // child was measured once already above ... base new measurement 737 // on stored values 738 int childWidth = child.getMeasuredWidth() + share; 739 if (childWidth < 0) { 740 childWidth = 0; 741 } 742 743 child.measure( 744 MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY), 745 childHeightMeasureSpec); 746 } else { 747 // child was skipped in the loop above. Measure for this first time here 748 child.measure(MeasureSpec.makeMeasureSpec( 749 share > 0 ? share : 0, MeasureSpec.EXACTLY), 750 childHeightMeasureSpec); 751 } 752 } 753 754 mTotalLength += child.getMeasuredWidth() + lp.leftMargin + 755 lp.rightMargin + getNextLocationOffset(child); 756 757 boolean matchHeightLocally = heightMode != MeasureSpec.EXACTLY && 758 lp.height == LayoutParams.FILL_PARENT; 759 760 final int margin = lp.topMargin + lp .bottomMargin; 761 int childHeight = child.getMeasuredHeight() + margin; 762 maxHeight = Math.max(maxHeight, childHeight); 763 alternativeMaxHeight = Math.max(alternativeMaxHeight, 764 matchHeightLocally ? margin : childHeight); 765 766 allFillParent = allFillParent && lp.height == LayoutParams.FILL_PARENT; 767 768 if (baselineAligned) { 769 final int childBaseline = child.getBaseline(); 770 if (childBaseline != -1) { 771 // Translates the child's vertical gravity into an index in the range 0..2 772 final int gravity = (lp.gravity < 0 ? mGravity : lp.gravity) 773 & Gravity.VERTICAL_GRAVITY_MASK; 774 final int index = ((gravity >> Gravity.AXIS_Y_SHIFT) 775 & ~Gravity.AXIS_SPECIFIED) >> 1; 776 777 maxAscent[index] = Math.max(maxAscent[index], childBaseline); 778 maxDescent[index] = Math.max(maxDescent[index], 779 childHeight - childBaseline); 780 } 781 } 782 } 783 784 // Add in our padding 785 mTotalLength += mPaddingLeft + mPaddingRight; 786 787 // Check mMaxAscent[INDEX_TOP] first because it maps to Gravity.TOP, 788 // the most common case 789 if (maxAscent[INDEX_TOP] != -1 || 790 maxAscent[INDEX_CENTER_VERTICAL] != -1 || 791 maxAscent[INDEX_BOTTOM] != -1 || 792 maxAscent[INDEX_FILL] != -1) { 793 final int ascent = Math.max(maxAscent[INDEX_FILL], 794 Math.max(maxAscent[INDEX_CENTER_VERTICAL], 795 Math.max(maxAscent[INDEX_TOP], maxAscent[INDEX_BOTTOM]))); 796 final int descent = Math.max(maxDescent[INDEX_FILL], 797 Math.max(maxDescent[INDEX_CENTER_VERTICAL], 798 Math.max(maxDescent[INDEX_TOP], maxDescent[INDEX_BOTTOM]))); 799 maxHeight = Math.max(maxHeight, ascent + descent); 800 } 801 } else { 802 alternativeMaxHeight = Math.max(alternativeMaxHeight, weightedMaxHeight); 803 } 804 805 if (!allFillParent && heightMode != MeasureSpec.EXACTLY) { 806 maxHeight = alternativeMaxHeight; 807 } 808 809 maxHeight += mPaddingTop + mPaddingBottom; 810 811 // Check against our minimum height 812 maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); 813 814 setMeasuredDimension(widthSize, resolveSize(maxHeight, heightMeasureSpec)); 815 816 if (matchHeight) { 817 forceUniformHeight(count, widthMeasureSpec); 818 } 819 } 820 821 private void forceUniformHeight(int count, int widthMeasureSpec) { 822 // Pretend that the linear layout has an exact size. This is the measured height of 823 // ourselves. The measured height should be the max height of the children, changed 824 // to accomodate the heightMesureSpec from the parent 825 int uniformMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), 826 MeasureSpec.EXACTLY); 827 for (int i = 0; i < count; ++i) { 828 final View child = getVirtualChildAt(i); 829 if (child.getVisibility() != GONE) { 830 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); 831 832 if (lp.height == LayoutParams.FILL_PARENT) { 833 // Temporarily force children to reuse their old measured width 834 // FIXME: this may not be right for something like wrapping text? 835 int oldWidth = lp.width; 836 lp.width = child.getMeasuredWidth(); 837 838 // Remeasure with new dimensions 839 measureChildWithMargins(child, widthMeasureSpec, 0, uniformMeasureSpec, 0); 840 lp.width = oldWidth; 841 } 842 } 843 } 844 } 845 846 /** 847 * <p>Returns the number of children to skip after measuring/laying out 848 * the specified child.</p> 849 * 850 * @param child the child after which we want to skip children 851 * @param index the index of the child after which we want to skip children 852 * @return the number of children to skip, 0 by default 853 */ 854 int getChildrenSkipCount(View child, int index) { 855 return 0; 856 } 857 858 /** 859 * <p>Returns the size (width or height) that should be occupied by a null 860 * child.</p> 861 * 862 * @param childIndex the index of the null child 863 * @return the width or height of the child depending on the orientation 864 */ 865 int measureNullChild(int childIndex) { 866 return 0; 867 } 868 869 /** 870 * <p>Measure the child according to the parent's measure specs. This 871 * method should be overriden by subclasses to force the sizing of 872 * children. This method is called by {@link #measureVertical(int, int)} and 873 * {@link #measureHorizontal(int, int)}.</p> 874 * 875 * @param child the child to measure 876 * @param childIndex the index of the child in this view 877 * @param widthMeasureSpec horizontal space requirements as imposed by the parent 878 * @param totalWidth extra space that has been used up by the parent horizontally 879 * @param heightMeasureSpec vertical space requirements as imposed by the parent 880 * @param totalHeight extra space that has been used up by the parent vertically 881 */ 882 void measureChildBeforeLayout(View child, int childIndex, 883 int widthMeasureSpec, int totalWidth, int heightMeasureSpec, 884 int totalHeight) { 885 measureChildWithMargins(child, widthMeasureSpec, totalWidth, 886 heightMeasureSpec, totalHeight); 887 } 888 889 /** 890 * <p>Return the location offset of the specified child. This can be used 891 * by subclasses to change the location of a given widget.</p> 892 * 893 * @param child the child for which to obtain the location offset 894 * @return the location offset in pixels 895 */ 896 int getLocationOffset(View child) { 897 return 0; 898 } 899 900 /** 901 * <p>Return the size offset of the next sibling of the specified child. 902 * This can be used by subclasses to change the location of the widget 903 * following <code>child</code>.</p> 904 * 905 * @param child the child whose next sibling will be moved 906 * @return the location offset of the next child in pixels 907 */ 908 int getNextLocationOffset(View child) { 909 return 0; 910 } 911 912 @Override 913 protected void onLayout(boolean changed, int l, int t, int r, int b) { 914 if (mOrientation == VERTICAL) { 915 layoutVertical(); 916 } else { 917 layoutHorizontal(); 918 } 919 } 920 921 /** 922 * Position the children during a layout pass if the orientation of this 923 * LinearLayout is set to {@link #VERTICAL}. 924 * 925 * @see #getOrientation() 926 * @see #setOrientation(int) 927 * @see #onLayout(boolean, int, int, int, int) 928 */ 929 void layoutVertical() { 930 final int paddingLeft = mPaddingLeft; 931 932 int childTop = mPaddingTop; 933 int childLeft = paddingLeft; 934 935 // Where right end of child should go 936 final int width = mRight - mLeft; 937 int childRight = width - mPaddingRight; 938 939 // Space available for child 940 int childSpace = width - paddingLeft - mPaddingRight; 941 942 final int count = getVirtualChildCount(); 943 944 final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; 945 final int minorGravity = mGravity & Gravity.HORIZONTAL_GRAVITY_MASK; 946 947 if (majorGravity != Gravity.TOP) { 948 switch (majorGravity) { 949 case Gravity.BOTTOM: 950 // mTotalLength contains the padding already, we add the top 951 // padding to compensate 952 childTop = mBottom - mTop + mPaddingTop - mTotalLength; 953 break; 954 955 case Gravity.CENTER_VERTICAL: 956 childTop += ((mBottom - mTop) - mTotalLength) / 2; 957 break; 958 } 959 960 } 961 962 for (int i = 0; i < count; i++) { 963 final View child = getVirtualChildAt(i); 964 if (child == null) { 965 childTop += measureNullChild(i); 966 } else if (child.getVisibility() != GONE) { 967 final int childWidth = child.getMeasuredWidth(); 968 final int childHeight = child.getMeasuredHeight(); 969 970 final LinearLayout.LayoutParams lp = 971 (LinearLayout.LayoutParams) child.getLayoutParams(); 972 973 int gravity = lp.gravity; 974 if (gravity < 0) { 975 gravity = minorGravity; 976 } 977 978 switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) { 979 case Gravity.LEFT: 980 childLeft = paddingLeft + lp.leftMargin; 981 break; 982 983 case Gravity.CENTER_HORIZONTAL: 984 childLeft = paddingLeft + ((childSpace - childWidth) / 2) 985 + lp.leftMargin - lp.rightMargin; 986 break; 987 988 case Gravity.RIGHT: 989 childLeft = childRight - childWidth - lp.rightMargin; 990 break; 991 } 992 993 994 childTop += lp.topMargin; 995 setChildFrame(child, childLeft, childTop + getLocationOffset(child), 996 childWidth, childHeight); 997 childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child); 998 999 i += getChildrenSkipCount(child, i); 1000 } 1001 } 1002 } 1003 1004 /** 1005 * Position the children during a layout pass if the orientation of this 1006 * LinearLayout is set to {@link #HORIZONTAL}. 1007 * 1008 * @see #getOrientation() 1009 * @see #setOrientation(int) 1010 * @see #onLayout(boolean, int, int, int, int) 1011 */ 1012 void layoutHorizontal() { 1013 final int paddingTop = mPaddingTop; 1014 1015 int childTop = paddingTop; 1016 int childLeft = mPaddingLeft; 1017 1018 // Where bottom of child should go 1019 final int height = mBottom - mTop; 1020 int childBottom = height - mPaddingBottom; 1021 1022 // Space available for child 1023 int childSpace = height - paddingTop - mPaddingBottom; 1024 1025 final int count = getVirtualChildCount(); 1026 1027 final int majorGravity = mGravity & Gravity.HORIZONTAL_GRAVITY_MASK; 1028 final int minorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; 1029 1030 final boolean baselineAligned = mBaselineAligned; 1031 1032 final int[] maxAscent = mMaxAscent; 1033 final int[] maxDescent = mMaxDescent; 1034 1035 if (majorGravity != Gravity.LEFT) { 1036 switch (majorGravity) { 1037 case Gravity.RIGHT: 1038 // mTotalLength contains the padding already, we add the left 1039 // padding to compensate 1040 childLeft = mRight - mLeft + mPaddingLeft - mTotalLength; 1041 break; 1042 1043 case Gravity.CENTER_HORIZONTAL: 1044 childLeft += ((mRight - mLeft) - mTotalLength) / 2; 1045 break; 1046 } 1047 } 1048 1049 for (int i = 0; i < count; i++) { 1050 final View child = getVirtualChildAt(i); 1051 1052 if (child == null) { 1053 childLeft += measureNullChild(i); 1054 } else if (child.getVisibility() != GONE) { 1055 final int childWidth = child.getMeasuredWidth(); 1056 final int childHeight = child.getMeasuredHeight(); 1057 int childBaseline = -1; 1058 1059 final LinearLayout.LayoutParams lp = 1060 (LinearLayout.LayoutParams) child.getLayoutParams(); 1061 1062 if (baselineAligned && lp.height != LayoutParams.FILL_PARENT) { 1063 childBaseline = child.getBaseline(); 1064 } 1065 1066 int gravity = lp.gravity; 1067 if (gravity < 0) { 1068 gravity = minorGravity; 1069 } 1070 1071 switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) { 1072 case Gravity.TOP: 1073 childTop = paddingTop + lp.topMargin; 1074 if (childBaseline != -1) { 1075 childTop += maxAscent[INDEX_TOP] - childBaseline; 1076 } 1077 break; 1078 1079 case Gravity.CENTER_VERTICAL: 1080 // Removed support for baselign alignment when layout_gravity or 1081 // gravity == center_vertical. See bug #1038483. 1082 // Keep the code around if we need to re-enable this feature 1083 // if (childBaseline != -1) { 1084 // // Align baselines vertically only if the child is smaller than us 1085 // if (childSpace - childHeight > 0) { 1086 // childTop = paddingTop + (childSpace / 2) - childBaseline; 1087 // } else { 1088 // childTop = paddingTop + (childSpace - childHeight) / 2; 1089 // } 1090 // } else { 1091 childTop = paddingTop + ((childSpace - childHeight) / 2) 1092 + lp.topMargin - lp.bottomMargin; 1093 break; 1094 1095 case Gravity.BOTTOM: 1096 childTop = childBottom - childHeight - lp.bottomMargin; 1097 if (childBaseline != -1) { 1098 int descent = child.getMeasuredHeight() - childBaseline; 1099 childTop -= (maxDescent[INDEX_BOTTOM] - descent); 1100 } 1101 break; 1102 } 1103 1104 childLeft += lp.leftMargin; 1105 setChildFrame(child, childLeft + getLocationOffset(child), childTop, 1106 childWidth, childHeight); 1107 childLeft += childWidth + lp.rightMargin + 1108 getNextLocationOffset(child); 1109 1110 i += getChildrenSkipCount(child, i); 1111 } 1112 } 1113 } 1114 1115 private void setChildFrame(View child, int left, int top, int width, int height) { 1116 child.layout(left, top, left + width, top + height); 1117 } 1118 1119 /** 1120 * Should the layout be a column or a row. 1121 * @param orientation Pass HORIZONTAL or VERTICAL. Default 1122 * value is HORIZONTAL. 1123 * 1124 * @attr ref android.R.styleable#LinearLayout_orientation 1125 */ 1126 public void setOrientation(int orientation) { 1127 if (mOrientation != orientation) { 1128 mOrientation = orientation; 1129 requestLayout(); 1130 } 1131 } 1132 1133 /** 1134 * Returns the current orientation. 1135 * 1136 * @return either {@link #HORIZONTAL} or {@link #VERTICAL} 1137 */ 1138 public int getOrientation() { 1139 return mOrientation; 1140 } 1141 1142 /** 1143 * Describes how the child views are positioned. Defaults to GRAVITY_TOP. If 1144 * this layout has a VERTICAL orientation, this controls where all the child 1145 * views are placed if there is extra vertical space. If this layout has a 1146 * HORIZONTAL orientation, this controls the alignment of the children. 1147 * 1148 * @param gravity See {@link android.view.Gravity} 1149 * 1150 * @attr ref android.R.styleable#LinearLayout_gravity 1151 */ 1152 public void setGravity(int gravity) { 1153 if (mGravity != gravity) { 1154 if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == 0) { 1155 gravity |= Gravity.LEFT; 1156 } 1157 1158 if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) { 1159 gravity |= Gravity.TOP; 1160 } 1161 1162 mGravity = gravity; 1163 requestLayout(); 1164 } 1165 } 1166 1167 public void setHorizontalGravity(int horizontalGravity) { 1168 final int gravity = horizontalGravity & Gravity.HORIZONTAL_GRAVITY_MASK; 1169 if ((mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) != gravity) { 1170 mGravity = (mGravity & ~Gravity.HORIZONTAL_GRAVITY_MASK) | gravity; 1171 requestLayout(); 1172 } 1173 } 1174 1175 public void setVerticalGravity(int verticalGravity) { 1176 final int gravity = verticalGravity & Gravity.VERTICAL_GRAVITY_MASK; 1177 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != gravity) { 1178 mGravity = (mGravity & ~Gravity.VERTICAL_GRAVITY_MASK) | gravity; 1179 requestLayout(); 1180 } 1181 } 1182 1183 @Override 1184 public LayoutParams generateLayoutParams(AttributeSet attrs) { 1185 return new LinearLayout.LayoutParams(getContext(), attrs); 1186 } 1187 1188 /** 1189 * Returns a set of layout parameters with a width of 1190 * {@link android.view.ViewGroup.LayoutParams#FILL_PARENT} 1191 * and a height of {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} 1192 * when the layout's orientation is {@link #VERTICAL}. When the orientation is 1193 * {@link #HORIZONTAL}, the width is set to {@link LayoutParams#WRAP_CONTENT} 1194 * and the height to {@link LayoutParams#WRAP_CONTENT}. 1195 */ 1196 @Override 1197 protected LayoutParams generateDefaultLayoutParams() { 1198 if (mOrientation == HORIZONTAL) { 1199 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 1200 } else if (mOrientation == VERTICAL) { 1201 return new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 1202 } 1203 return null; 1204 } 1205 1206 @Override 1207 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { 1208 return new LayoutParams(p); 1209 } 1210 1211 1212 // Override to allow type-checking of LayoutParams. 1213 @Override 1214 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { 1215 return p instanceof LinearLayout.LayoutParams; 1216 } 1217 1218 /** 1219 * Per-child layout information associated with ViewLinearLayout. 1220 * 1221 * @attr ref android.R.styleable#LinearLayout_Layout_layout_weight 1222 * @attr ref android.R.styleable#LinearLayout_Layout_layout_gravity 1223 */ 1224 public static class LayoutParams extends ViewGroup.MarginLayoutParams { 1225 /** 1226 * Indicates how much of the extra space in the LinearLayout will be 1227 * allocated to the view associated with these LayoutParams. Specify 1228 * 0 if the view should not be stretched. Otherwise the extra pixels 1229 * will be pro-rated among all views whose weight is greater than 0. 1230 */ 1231 @ViewDebug.ExportedProperty 1232 public float weight; 1233 1234 /** 1235 * Gravity for the view associated with these LayoutParams. 1236 * 1237 * @see android.view.Gravity 1238 */ 1239 @ViewDebug.ExportedProperty(mapping = { 1240 @ViewDebug.IntToString(from = -1, to = "NONE"), 1241 @ViewDebug.IntToString(from = Gravity.NO_GRAVITY, to = "NONE"), 1242 @ViewDebug.IntToString(from = Gravity.TOP, to = "TOP"), 1243 @ViewDebug.IntToString(from = Gravity.BOTTOM, to = "BOTTOM"), 1244 @ViewDebug.IntToString(from = Gravity.LEFT, to = "LEFT"), 1245 @ViewDebug.IntToString(from = Gravity.RIGHT, to = "RIGHT"), 1246 @ViewDebug.IntToString(from = Gravity.CENTER_VERTICAL, to = "CENTER_VERTICAL"), 1247 @ViewDebug.IntToString(from = Gravity.FILL_VERTICAL, to = "FILL_VERTICAL"), 1248 @ViewDebug.IntToString(from = Gravity.CENTER_HORIZONTAL, to = "CENTER_HORIZONTAL"), 1249 @ViewDebug.IntToString(from = Gravity.FILL_HORIZONTAL, to = "FILL_HORIZONTAL"), 1250 @ViewDebug.IntToString(from = Gravity.CENTER, to = "CENTER"), 1251 @ViewDebug.IntToString(from = Gravity.FILL, to = "FILL") 1252 }) 1253 public int gravity = -1; 1254 1255 /** 1256 * {@inheritDoc} 1257 */ 1258 public LayoutParams(Context c, AttributeSet attrs) { 1259 super(c, attrs); 1260 TypedArray a = 1261 c.obtainStyledAttributes(attrs, com.android.internal.R.styleable.LinearLayout_Layout); 1262 1263 weight = a.getFloat(com.android.internal.R.styleable.LinearLayout_Layout_layout_weight, 0); 1264 gravity = a.getInt(com.android.internal.R.styleable.LinearLayout_Layout_layout_gravity, -1); 1265 1266 a.recycle(); 1267 } 1268 1269 /** 1270 * {@inheritDoc} 1271 */ 1272 public LayoutParams(int width, int height) { 1273 super(width, height); 1274 weight = 0; 1275 } 1276 1277 /** 1278 * Creates a new set of layout parameters with the specified width, height 1279 * and weight. 1280 * 1281 * @param width the width, either {@link #FILL_PARENT}, 1282 * {@link #WRAP_CONTENT} or a fixed size in pixels 1283 * @param height the height, either {@link #FILL_PARENT}, 1284 * {@link #WRAP_CONTENT} or a fixed size in pixels 1285 * @param weight the weight 1286 */ 1287 public LayoutParams(int width, int height, float weight) { 1288 super(width, height); 1289 this.weight = weight; 1290 } 1291 1292 /** 1293 * {@inheritDoc} 1294 */ 1295 public LayoutParams(ViewGroup.LayoutParams p) { 1296 super(p); 1297 } 1298 1299 /** 1300 * {@inheritDoc} 1301 */ 1302 public LayoutParams(MarginLayoutParams source) { 1303 super(source); 1304 } 1305 1306 @Override 1307 public String debug(String output) { 1308 return output + "LinearLayout.LayoutParams={width=" + sizeToString(width) + 1309 ", height=" + sizeToString(height) + " weight=" + weight + "}"; 1310 } 1311 } 1312} 1313