| 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.AttrRes; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.StyleRes; |
| 23 |
import android.compat.annotation.UnsupportedAppUsage; |
| 24 |
import android.content.Context; |
| 25 |
import android.content.res.TypedArray; |
| 26 |
import android.graphics.Rect; |
| 27 |
import android.graphics.drawable.Drawable; |
| 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.util.ArrayList; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
@RemoteView |
| 59 |
public class FrameLayout extends ViewGroup { |
| 60 |
private static final int DEFAULT_CHILD_GRAVITY = Gravity.TOP | Gravity.START; |
| 61 |
|
| 62 |
@ViewDebug.ExportedProperty(category = "measurement") |
| 63 |
@UnsupportedAppUsage |
| 64 |
boolean mMeasureAllChildren = false; |
| 65 |
|
| 66 |
@ViewDebug.ExportedProperty(category = "padding") |
| 67 |
@UnsupportedAppUsage |
| 68 |
private int mForegroundPaddingLeft = 0; |
| 69 |
|
| 70 |
@ViewDebug.ExportedProperty(category = "padding") |
| 71 |
@UnsupportedAppUsage |
| 72 |
private int mForegroundPaddingTop = 0; |
| 73 |
|
| 74 |
@ViewDebug.ExportedProperty(category = "padding") |
| 75 |
@UnsupportedAppUsage |
| 76 |
private int mForegroundPaddingRight = 0; |
| 77 |
|
| 78 |
@ViewDebug.ExportedProperty(category = "padding") |
| 79 |
@UnsupportedAppUsage |
| 80 |
private int mForegroundPaddingBottom = 0; |
| 81 |
|
| 82 |
private final ArrayList<View> mMatchParentChildren = new ArrayList<>(1); |
| 83 |
|
| 84 |
public FrameLayout(@NonNull Context context) { |
| 85 |
super(context); |
| 86 |
} |
| 87 |
|
| 88 |
public FrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) { |
| 89 |
this(context, attrs, 0); |
| 90 |
} |
| 91 |
|
| 92 |
public FrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, |
| 93 |
@AttrRes int defStyleAttr) { |
| 94 |
this(context, attrs, defStyleAttr, 0); |
| 95 |
} |
| 96 |
|
| 97 |
public FrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, |
| 98 |
@AttrRes int defStyleAttr, @StyleRes int defStyleRes) { |
| 99 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 100 |
|
| 101 |
final TypedArray a = context.obtainStyledAttributes( |
| 102 |
attrs, R.styleable.FrameLayout, defStyleAttr, defStyleRes); |
| 103 |
saveAttributeDataForStyleable(context, R.styleable.FrameLayout, |
| 104 |
attrs, a, defStyleAttr, defStyleRes); |
| 105 |
|
| 106 |
if (a.getBoolean(R.styleable.FrameLayout_measureAllChildren, false)) { |
| 107 |
setMeasureAllChildren(true); |
| 108 |
} |
| 109 |
|
| 110 |
a.recycle(); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
@android.view.RemotableViewMethod |
| 123 |
public void setForegroundGravity(int foregroundGravity) { |
| 124 |
if (getForegroundGravity() != foregroundGravity) { |
| 125 |
super.setForegroundGravity(foregroundGravity); |
| 126 |
|
| 127 |
|
| 128 |
final Drawable foreground = getForeground(); |
| 129 |
if (getForegroundGravity() == Gravity.FILL && foreground != null) { |
| 130 |
Rect padding = new Rect(); |
| 131 |
if (foreground.getPadding(padding)) { |
| 132 |
mForegroundPaddingLeft = padding.left; |
| 133 |
mForegroundPaddingTop = padding.top; |
| 134 |
mForegroundPaddingRight = padding.right; |
| 135 |
mForegroundPaddingBottom = padding.bottom; |
| 136 |
} |
| 137 |
} else { |
| 138 |
mForegroundPaddingLeft = 0; |
| 139 |
mForegroundPaddingTop = 0; |
| 140 |
mForegroundPaddingRight = 0; |
| 141 |
mForegroundPaddingBottom = 0; |
| 142 |
} |
| 143 |
|
| 144 |
requestLayout(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
@Override |
| 154 |
protected LayoutParams generateDefaultLayoutParams() { |
| 155 |
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
| 156 |
} |
| 157 |
|
| 158 |
int getPaddingLeftWithForeground() { |
| 159 |
return isForegroundInsidePadding() ? Math.max(mPaddingLeft, mForegroundPaddingLeft) : |
| 160 |
mPaddingLeft + mForegroundPaddingLeft; |
| 161 |
} |
| 162 |
|
| 163 |
int getPaddingRightWithForeground() { |
| 164 |
return isForegroundInsidePadding() ? Math.max(mPaddingRight, mForegroundPaddingRight) : |
| 165 |
mPaddingRight + mForegroundPaddingRight; |
| 166 |
} |
| 167 |
|
| 168 |
private int getPaddingTopWithForeground() { |
| 169 |
return isForegroundInsidePadding() ? Math.max(mPaddingTop, mForegroundPaddingTop) : |
| 170 |
mPaddingTop + mForegroundPaddingTop; |
| 171 |
} |
| 172 |
|
| 173 |
private int getPaddingBottomWithForeground() { |
| 174 |
return isForegroundInsidePadding() ? Math.max(mPaddingBottom, mForegroundPaddingBottom) : |
| 175 |
mPaddingBottom + mForegroundPaddingBottom; |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 180 |
int count = getChildCount(); |
| 181 |
|
| 182 |
final boolean measureMatchParentChildren = |
| 183 |
MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY || |
| 184 |
MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY; |
| 185 |
mMatchParentChildren.clear(); |
| 186 |
|
| 187 |
int maxHeight = 0; |
| 188 |
int maxWidth = 0; |
| 189 |
int childState = 0; |
| 190 |
|
| 191 |
for (int i = 0; i < count; i++) { |
| 192 |
final View child = getChildAt(i); |
| 193 |
if (mMeasureAllChildren || child.getVisibility() != GONE) { |
| 194 |
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); |
| 195 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 196 |
maxWidth = Math.max(maxWidth, |
| 197 |
child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); |
| 198 |
maxHeight = Math.max(maxHeight, |
| 199 |
child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin); |
| 200 |
childState = combineMeasuredStates(childState, child.getMeasuredState()); |
| 201 |
if (measureMatchParentChildren) { |
| 202 |
if (lp.width == LayoutParams.MATCH_PARENT || |
| 203 |
lp.height == LayoutParams.MATCH_PARENT) { |
| 204 |
mMatchParentChildren.add(child); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground(); |
| 212 |
maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground(); |
| 213 |
|
| 214 |
|
| 215 |
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); |
| 216 |
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); |
| 217 |
|
| 218 |
|
| 219 |
final Drawable drawable = getForeground(); |
| 220 |
if (drawable != null) { |
| 221 |
maxHeight = Math.max(maxHeight, drawable.getMinimumHeight()); |
| 222 |
maxWidth = Math.max(maxWidth, drawable.getMinimumWidth()); |
| 223 |
} |
| 224 |
|
| 225 |
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), |
| 226 |
resolveSizeAndState(maxHeight, heightMeasureSpec, |
| 227 |
childState << MEASURED_HEIGHT_STATE_SHIFT)); |
| 228 |
|
| 229 |
count = mMatchParentChildren.size(); |
| 230 |
if (count > 1) { |
| 231 |
for (int i = 0; i < count; i++) { |
| 232 |
final View child = mMatchParentChildren.get(i); |
| 233 |
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); |
| 234 |
|
| 235 |
final int childWidthMeasureSpec; |
| 236 |
if (lp.width == LayoutParams.MATCH_PARENT) { |
| 237 |
final int width = Math.max(0, getMeasuredWidth() |
| 238 |
- getPaddingLeftWithForeground() - getPaddingRightWithForeground() |
| 239 |
- lp.leftMargin - lp.rightMargin); |
| 240 |
childWidthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 241 |
width, MeasureSpec.EXACTLY); |
| 242 |
} else { |
| 243 |
childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, |
| 244 |
getPaddingLeftWithForeground() + getPaddingRightWithForeground() + |
| 245 |
lp.leftMargin + lp.rightMargin, |
| 246 |
lp.width); |
| 247 |
} |
| 248 |
|
| 249 |
final int childHeightMeasureSpec; |
| 250 |
if (lp.height == LayoutParams.MATCH_PARENT) { |
| 251 |
final int height = Math.max(0, getMeasuredHeight() |
| 252 |
- getPaddingTopWithForeground() - getPaddingBottomWithForeground() |
| 253 |
- lp.topMargin - lp.bottomMargin); |
| 254 |
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 255 |
height, MeasureSpec.EXACTLY); |
| 256 |
} else { |
| 257 |
childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, |
| 258 |
getPaddingTopWithForeground() + getPaddingBottomWithForeground() + |
| 259 |
lp.topMargin + lp.bottomMargin, |
| 260 |
lp.height); |
| 261 |
} |
| 262 |
|
| 263 |
child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 270 |
layoutChildren(left, top, right, bottom, false ); |
| 271 |
} |
| 272 |
|
| 273 |
void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) { |
| 274 |
final int count = getChildCount(); |
| 275 |
|
| 276 |
final int parentLeft = getPaddingLeftWithForeground(); |
| 277 |
final int parentRight = right - left - getPaddingRightWithForeground(); |
| 278 |
|
| 279 |
final int parentTop = getPaddingTopWithForeground(); |
| 280 |
final int parentBottom = bottom - top - getPaddingBottomWithForeground(); |
| 281 |
|
| 282 |
for (int i = 0; i < count; i++) { |
| 283 |
final View child = getChildAt(i); |
| 284 |
if (child.getVisibility() != GONE) { |
| 285 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 286 |
|
| 287 |
final int width = child.getMeasuredWidth(); |
| 288 |
final int height = child.getMeasuredHeight(); |
| 289 |
|
| 290 |
int childLeft; |
| 291 |
int childTop; |
| 292 |
|
| 293 |
int gravity = lp.gravity; |
| 294 |
if (gravity == -1) { |
| 295 |
gravity = DEFAULT_CHILD_GRAVITY; |
| 296 |
} |
| 297 |
|
| 298 |
final int layoutDirection = getLayoutDirection(); |
| 299 |
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection); |
| 300 |
final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 301 |
|
| 302 |
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { |
| 303 |
case Gravity.CENTER_HORIZONTAL: |
| 304 |
childLeft = parentLeft + (parentRight - parentLeft - width) / 2 + |
| 305 |
lp.leftMargin - lp.rightMargin; |
| 306 |
break; |
| 307 |
case Gravity.RIGHT: |
| 308 |
if (!forceLeftGravity) { |
| 309 |
childLeft = parentRight - width - lp.rightMargin; |
| 310 |
break; |
| 311 |
} |
| 312 |
case Gravity.LEFT: |
| 313 |
default: |
| 314 |
childLeft = parentLeft + lp.leftMargin; |
| 315 |
} |
| 316 |
|
| 317 |
switch (verticalGravity) { |
| 318 |
case Gravity.TOP: |
| 319 |
childTop = parentTop + lp.topMargin; |
| 320 |
break; |
| 321 |
case Gravity.CENTER_VERTICAL: |
| 322 |
childTop = parentTop + (parentBottom - parentTop - height) / 2 + |
| 323 |
lp.topMargin - lp.bottomMargin; |
| 324 |
break; |
| 325 |
case Gravity.BOTTOM: |
| 326 |
childTop = parentBottom - height - lp.bottomMargin; |
| 327 |
break; |
| 328 |
default: |
| 329 |
childTop = parentTop + lp.topMargin; |
| 330 |
} |
| 331 |
|
| 332 |
child.layout(childLeft, childTop, childLeft + width, childTop + height); |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
@android.view.RemotableViewMethod |
| 347 |
public void setMeasureAllChildren(boolean measureAll) { |
| 348 |
mMeasureAllChildren = measureAll; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
@Deprecated |
| 363 |
public boolean getConsiderGoneChildrenWhenMeasuring() { |
| 364 |
return getMeasureAllChildren(); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
@InspectableProperty |
| 374 |
public boolean getMeasureAllChildren() { |
| 375 |
return mMeasureAllChildren; |
| 376 |
} |
| 377 |
|
| 378 |
@Override |
| 379 |
public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 380 |
return new FrameLayout.LayoutParams(getContext(), attrs); |
| 381 |
} |
| 382 |
|
| 383 |
@Override |
| 384 |
public boolean shouldDelayChildPressedState() { |
| 385 |
return false; |
| 386 |
} |
| 387 |
|
| 388 |
@Override |
| 389 |
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 390 |
return p instanceof LayoutParams; |
| 391 |
} |
| 392 |
|
| 393 |
@Override |
| 394 |
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { |
| 395 |
if (sPreserveMarginParamsInLayoutParamConversion) { |
| 396 |
if (lp instanceof LayoutParams) { |
| 397 |
return new LayoutParams((LayoutParams) lp); |
| 398 |
} else if (lp instanceof MarginLayoutParams) { |
| 399 |
return new LayoutParams((MarginLayoutParams) lp); |
| 400 |
} |
| 401 |
} |
| 402 |
return new LayoutParams(lp); |
| 403 |
} |
| 404 |
|
| 405 |
@Override |
| 406 |
public CharSequence getAccessibilityClassName() { |
| 407 |
return FrameLayout.class.getName(); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
@Override |
| 412 |
protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 413 |
super.encodeProperties(encoder); |
| 414 |
|
| 415 |
encoder.addProperty("measurement:measureAllChildren", mMeasureAllChildren); |
| 416 |
encoder.addProperty("padding:foregroundPaddingLeft", mForegroundPaddingLeft); |
| 417 |
encoder.addProperty("padding:foregroundPaddingTop", mForegroundPaddingTop); |
| 418 |
encoder.addProperty("padding:foregroundPaddingRight", mForegroundPaddingRight); |
| 419 |
encoder.addProperty("padding:foregroundPaddingBottom", mForegroundPaddingBottom); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public static class LayoutParams extends MarginLayoutParams { |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
public static final int UNSPECIFIED_GRAVITY = -1; |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
@InspectableProperty( |
| 447 |
name = "layout_gravity", |
| 448 |
valueType = InspectableProperty.ValueType.GRAVITY) |
| 449 |
public int gravity = UNSPECIFIED_GRAVITY; |
| 450 |
|
| 451 |
public LayoutParams(@NonNull Context c, @Nullable AttributeSet attrs) { |
| 452 |
super(c, attrs); |
| 453 |
|
| 454 |
final TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.FrameLayout_Layout); |
| 455 |
gravity = a.getInt(R.styleable.FrameLayout_Layout_layout_gravity, UNSPECIFIED_GRAVITY); |
| 456 |
a.recycle(); |
| 457 |
} |
| 458 |
|
| 459 |
public LayoutParams(int width, int height) { |
| 460 |
super(width, height); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
public LayoutParams(int width, int height, int gravity) { |
| 476 |
super(width, height); |
| 477 |
this.gravity = gravity; |
| 478 |
} |
| 479 |
|
| 480 |
public LayoutParams(@NonNull ViewGroup.LayoutParams source) { |
| 481 |
super(source); |
| 482 |
} |
| 483 |
|
| 484 |
public LayoutParams(@NonNull ViewGroup.MarginLayoutParams source) { |
| 485 |
super(source); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
public LayoutParams(@NonNull LayoutParams source) { |
| 495 |
super(source); |
| 496 |
|
| 497 |
this.gravity = source.gravity; |
| 498 |
} |
| 499 |
} |
| 500 |
} |