下面列出了android.view.Gravity#FILL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Describes how the foreground is positioned. Defaults to START and TOP.
*
* @param foregroundGravity See {@link android.view.Gravity}
*
* @see #getForegroundGravity()
*
* @attr ref android.R.styleable#View_foregroundGravity
*/
@android.view.RemotableViewMethod
public void setForegroundGravity(int foregroundGravity) {
if (getForegroundGravity() != foregroundGravity) {
super.setForegroundGravity(foregroundGravity);
// calling get* again here because the set above may apply default constraints
final Drawable foreground = getForeground();
if (getForegroundGravity() == Gravity.FILL && foreground != null) {
Rect padding = new Rect();
if (foreground.getPadding(padding)) {
mForegroundPaddingLeft = padding.left;
mForegroundPaddingTop = padding.top;
mForegroundPaddingRight = padding.right;
mForegroundPaddingBottom = padding.bottom;
}
} else {
mForegroundPaddingLeft = 0;
mForegroundPaddingTop = 0;
mForegroundPaddingRight = 0;
mForegroundPaddingBottom = 0;
}
requestLayout();
}
}
public void setForegroundGravity(View view, int foregroundGravity) {
if (view != null) {
if (mForegroundGravity != foregroundGravity) {
if ((foregroundGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.START;
}
if ((foregroundGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.TOP;
}
mForegroundGravity = foregroundGravity;
if (mForegroundGravity == Gravity.FILL && mForeground != null) {
Rect padding = new Rect();
mForeground.getPadding(padding);
}
view.requestLayout();
}
}
}
@Override
public void setForeground(Drawable foreground) {
if (foregroundDrawable != null) {
foregroundDrawable.setCallback(null);
unscheduleDrawable(foregroundDrawable);
}
foregroundDrawable = foreground;
updateForegroundColor();
if (foreground != null) {
setWillNotDraw(false);
foreground.setCallback(this);
if (foreground.isStateful()) {
foreground.setState(getDrawableState());
}
if (foregroundDrawGravity == Gravity.FILL) {
Rect padding = new Rect();
foreground.getPadding(padding);
}
}
requestLayout();
invalidate();
}
@Override
public void setForegroundGravity(int foregroundGravity) {
if (foregroundDrawGravity != foregroundGravity) {
if ((foregroundGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
foregroundGravity = foregroundGravity | Gravity.START;
}
if ((foregroundGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
foregroundGravity = foregroundGravity | Gravity.TOP;
}
foregroundDrawGravity = foregroundGravity;
if (foregroundDrawGravity == Gravity.FILL && foregroundDrawable != null) {
Rect padding = new Rect();
foregroundDrawable.getPadding(padding);
}
requestLayout();
}
}
public VerticalTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
mTabSelectedListeners = new ArrayList<>();
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.VerticalTabLayout);
mColorIndicator = typedArray.getColor(R.styleable.VerticalTabLayout_indicator_color,
context.getResources().getColor(R.color.colorAccent));
mIndicatorWidth = (int) typedArray.getDimension(R.styleable.VerticalTabLayout_indicator_width, DisplayUtil.dp2px(context, 3));
mIndicatorCorners = typedArray.getDimension(R.styleable.VerticalTabLayout_indicator_corners, 0);
mIndicatorGravity = typedArray.getInteger(R.styleable.VerticalTabLayout_indicator_gravity, Gravity.LEFT);
if (mIndicatorGravity == 3) {
mIndicatorGravity = Gravity.LEFT;
} else if (mIndicatorGravity == 5) {
mIndicatorGravity = Gravity.RIGHT;
} else if (mIndicatorGravity == 119) {
mIndicatorGravity = Gravity.FILL;
}
mTabMargin = (int) typedArray.getDimension(R.styleable.VerticalTabLayout_tab_margin, 0);
mTabMode = typedArray.getInteger(R.styleable.VerticalTabLayout_tab_mode, TAB_MODE_FIXED);
int defaultTabHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
mTabHeight = (int) typedArray.getDimension(R.styleable.VerticalTabLayout_tab_height, defaultTabHeight);
typedArray.recycle();
}
public void setForegroundGravity(int foregroundGravity) {
if (mForegroundGravity != foregroundGravity) {
if ((foregroundGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.START;
}
if ((foregroundGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.TOP;
}
mForegroundGravity = foregroundGravity;
if (mForegroundGravity == Gravity.FILL && mForeground != null) {
Rect padding = new Rect();
mForeground.getPadding(padding);
}
mView.requestLayout();
}
}
/**
* Describes how the foreground is positioned. Defaults to START and TOP.
*
* @param foregroundGravity See {@link Gravity}
*
* @see #getForegroundGravity()
*/
public void setForegroundGravity(int foregroundGravity) {
if (mForegroundGravity != foregroundGravity) {
if ((foregroundGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.START;
}
if ((foregroundGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.TOP;
}
mForegroundGravity = foregroundGravity;
if (mForegroundGravity == Gravity.FILL && mForeground != null) {
Rect padding = new Rect();
mForeground.getPadding(padding);
}
requestLayout();
}
}
/**
* Describes how the foreground is positioned. Defaults to START and TOP.
*
* @param foregroundGravity See {@link android.view.Gravity}
*
* @see #getForegroundGravity()
*/
public void setForegroundGravity(int foregroundGravity) {
if (mForegroundGravity != foregroundGravity) {
if ((foregroundGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.START;
}
if ((foregroundGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.TOP;
}
mForegroundGravity = foregroundGravity;
if (mForegroundGravity == Gravity.FILL && mWheelForeground != null) {
Rect padding = new Rect();
mWheelForeground.getPadding(padding);
}
requestLayout();
}
}
private static String gravityToStr(int gravity) {
switch (gravity) {
case Gravity.NO_GRAVITY:
return "NO_GRAVITY";
case Gravity.LEFT:
return "LEFT";
case Gravity.TOP:
return "TOP";
case Gravity.RIGHT:
return "RIGHT";
case Gravity.BOTTOM:
return "BOTTOM";
case Gravity.CENTER:
return "CENTER";
case Gravity.CENTER_HORIZONTAL:
return "CENTER_HORIZONTAL";
case Gravity.CENTER_VERTICAL:
return "CENTER_VERTICAL";
case Gravity.START:
return "START";
case Gravity.END:
return "END";
case Gravity.CLIP_HORIZONTAL:
return "CLIP_HORIZONTAL";
case Gravity.CLIP_VERTICAL:
return "CLIP_VERTICAL";
case Gravity.FILL:
return "FILL";
case Gravity.FILL_HORIZONTAL:
return "FILL_HORIZONTAL";
case Gravity.FILL_VERTICAL:
return "FILL_VERTICAL";
default:
return "OTHER";
}
}
/**
* Supply a Drawable that is to be rendered on top of all of the child
* views in the frame layout. Any padding in the Drawable will be taken
* into account by ensuring that the children are inset to be placed
* inside of the padding area.
*
* @param drawable The Drawable to be drawn on top of the children.
*/
public void setForeground(Drawable drawable) {
if (mForeground != drawable) {
if (mForeground != null) {
mForeground.setCallback(null);
unscheduleDrawable(mForeground);
}
mForeground = drawable;
if (drawable != null) {
setWillNotDraw(false);
drawable.setCallback(this);
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
if (mForegroundGravity == Gravity.FILL) {
Rect padding = new Rect();
drawable.getPadding(padding);
}
} else {
setWillNotDraw(true);
}
requestLayout();
invalidate();
}
}
/**
* Supply a Drawable that is to be rendered on top of all of the child
* views in the frame layout. Any padding in the Drawable will be taken
* into account by ensuring that the children are inset to be placed
* inside of the padding area.
*
* @param drawable The Drawable to be drawn on top of the children.
*/
public void setForeground(View view, Drawable drawable) {
if (view != null) {
if (mForeground != drawable) {
if (mForeground != null) {
mForeground.setCallback(null);
view.unscheduleDrawable(mForeground);
}
mForeground = drawable;
if (drawable != null) {
view.setWillNotDraw(false);
drawable.setCallback(view);
if (drawable.isStateful()) {
drawable.setState(view.getDrawableState());
}
if (mForegroundGravity == Gravity.FILL) {
Rect padding = new Rect();
drawable.getPadding(padding);
}
//update bounds
updateBounds(view, drawable);//added by song
} else {
view.setWillNotDraw(true);
}
view.requestLayout();
view.invalidate();
}
}
}
protected static int getGravity(View view) {
int gravity = -1;
LayoutParams params = view.getLayoutParams();
if (params instanceof FrameLayout.LayoutParams) {
gravity = ((FrameLayout.LayoutParams)params).gravity;
}
if (gravity == -1) {
gravity = Gravity.FILL;
}
return gravity;
}
/**
* Describes how the foreground is positioned. Defaults to START and TOP.
*
* @param foregroundGravity See {@link android.view.Gravity}
*
* @see #getForegroundGravity()
*
* @attr ref android.R.styleable#FrameLayout_foregroundGravity
*/
public void setForegroundGravity(int foregroundGravity) {
if (mForegroundGravity != foregroundGravity) {
if ((foregroundGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.START;
}
if ((foregroundGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.TOP;
}
mForegroundGravity = foregroundGravity;
if (mForegroundGravity == Gravity.FILL && mForeground != null) {
Rect padding = new Rect();
if (mForeground.getPadding(padding)) {
mForegroundPaddingLeft = padding.left;
mForegroundPaddingTop = padding.top;
mForegroundPaddingRight = padding.right;
mForegroundPaddingBottom = padding.bottom;
}
} else {
mForegroundPaddingLeft = 0;
mForegroundPaddingTop = 0;
mForegroundPaddingRight = 0;
mForegroundPaddingBottom = 0;
}
requestLayout();
}
}
/**
* Supply a Drawable that is to be rendered on top of all of the child
* views in the frame layout. Any padding in the Drawable will be taken
* into account by ensuring that the children are inset to be placed
* inside of the padding area.
*
* @param drawable The Drawable to be drawn on top of the children.
*/
public void setForeground(@Nullable Drawable drawable) {
if (mForeground != drawable) {
if (mForeground != null) {
mForeground.setCallback(null);
mView.unscheduleDrawable(mForeground);
}
mForeground = drawable;
if (drawable != null) {
mView.setWillNotDraw(false);
drawable.setCallback(mView);
if (drawable.isStateful()) {
drawable.setState(mView.getDrawableState());
}
if (mForegroundGravity == Gravity.FILL) {
Rect padding = new Rect();
drawable.getPadding(padding);
}
} else {
mView.setWillNotDraw(true);
}
mView.requestLayout();
mView.invalidate();
}
}
/**
* Supply a Drawable that is to be rendered on top of all of the child views in the frame layout.
* Any padding in the Drawable will be taken into account by ensuring that the children are inset
* to be placed inside of the padding area.
*
* @param drawable The Drawable to be drawn on top of the children.
*/
@Override
public void setForeground(@Nullable Drawable drawable) {
if (foreground != drawable) {
if (foreground != null) {
foreground.setCallback(null);
unscheduleDrawable(foreground);
}
foreground = drawable;
if (drawable != null) {
setWillNotDraw(false);
drawable.setCallback(this);
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
if (foregroundGravity == Gravity.FILL) {
Rect padding = new Rect();
drawable.getPadding(padding);
}
} else {
setWillNotDraw(true);
}
requestLayout();
invalidate();
}
}
/**
* Supply a Drawable that is to be rendered on top of all of the child
* views in this layout. Any padding in the Drawable will be taken
* into account by ensuring that the children are inset to be placed
* inside of the padding area.
*
* @param drawable The Drawable to be drawn on top of the children.
*/
public void setForeground(Drawable drawable) {
if (mForeground != drawable) {
if (mForeground != null) {
mForeground.setCallback(null);
unscheduleDrawable(mForeground);
}
mForeground = drawable;
if (drawable != null) {
setWillNotDraw(false);
drawable.setCallback(this);
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
if (mForegroundGravity == Gravity.FILL) {
Rect padding = new Rect();
drawable.getPadding(padding);
}
} else {
setWillNotDraw(true);
}
requestLayout();
invalidate();
}
}
/**
* Supply a Drawable that is to be rendered on top of all of the child
* views in the frame layout. Any padding in the Drawable will be taken
* into account by ensuring that the children are inset to be placed
* inside of the padding area.
*
* @param drawable The Drawable to be drawn on top of the children.
*/
public void setForeground(Drawable drawable) {
if (mForeground != drawable) {
if (mForeground != null) {
mForeground.setCallback(null);
unscheduleDrawable(mForeground);
}
mForeground = drawable;
if (drawable != null) {
setWillNotDraw(false);
drawable.setCallback(this);
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
if (mForegroundGravity == Gravity.FILL) {
Rect padding = new Rect();
drawable.getPadding(padding);
}
} else {
setWillNotDraw(true);
}
requestLayout();
invalidate();
}
}
/**
* {@inheritDoc} Does a quick and rough parsing of the raw string for containing constant words like
* <code>top</code> or <code>center_vertical</code>
*/
@Override
@SuppressLint("RtlHardcoded")
public int getGravity(String key, int defaultValue) {
String gravityArg = resolveArgument(key);
if (gravityArg == null) {
return defaultValue;
} else if (gravityArg.equals("center")) {
return Gravity.CENTER;
} else if (gravityArg.equals("fill")) {
return Gravity.FILL;
} else {
// supported options
int gravity = 0;
if (gravityArg.contains("top")) {
gravity |= Gravity.TOP;
}
if (gravityArg.contains("bottom")) {
gravity |= Gravity.BOTTOM;
}
if (gravityArg.contains("center_vertical")) {
gravity |= Gravity.CENTER_VERTICAL;
}
if (gravityArg.contains("fill_vertical")) {
gravity |= Gravity.FILL_VERTICAL;
}
if (gravityArg.contains("left")) {
gravity |= Gravity.LEFT;
}
if (gravityArg.contains("right")) {
gravity |= Gravity.RIGHT;
}
if (gravityArg.contains("center_horizontal")) {
gravity |= Gravity.CENTER_HORIZONTAL;
}
if (gravityArg.contains("fill_horizontal")) {
gravity |= Gravity.FILL_HORIZONTAL;
}
return gravity;
}
}
@SuppressLint("RtlHardcoded")
private void applyGravity(int itemWidth, int itemHeight) {
final int count = mCount;
if (count == 0) {
mFirstCenterX = 0;
mFirstCenterY = 0;
mItemCenterOffset = 0;
return;
}
final int padding = mPadding;
final float scale = mScale > 1 ? mScale : 1;
final int paddingStart = ViewCompat.getPaddingStart(this);
final int paddingEnd = ViewCompat.getPaddingEnd(this);
final int paddingTop = getPaddingTop();
final int paddingBottom = getPaddingBottom();
final int width = getMeasuredWidth();
final int height = getMeasuredHeight();
final int contentWidth = width - paddingStart - paddingEnd;
final int contentHeight = height - paddingTop - paddingBottom;
mItemCenterOffset = itemWidth + padding;
switch (GravityCompat.getAbsoluteGravity(mGravity,
ViewCompat.getLayoutDirection(this))) {
case Gravity.LEFT:
case Gravity.TOP:
case Gravity.LEFT | Gravity.TOP:
mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
mFirstCenterY = paddingTop + itemHeight * scale * 0.5f;
break;
case Gravity.CENTER_HORIZONTAL:
case Gravity.CENTER_HORIZONTAL | Gravity.TOP:
mFirstCenterX = paddingStart + contentWidth * 0.5f -
mItemCenterOffset * (count - 1) * 0.5f;
mFirstCenterY = paddingTop + itemHeight * scale * 0.5f;
break;
case Gravity.RIGHT:
case Gravity.RIGHT | Gravity.TOP:
mFirstCenterX = width - paddingEnd - itemWidth * scale * 0.5f -
mItemCenterOffset * (count - 1);
mFirstCenterY = paddingTop + itemHeight * scale * 0.5f;
break;
case Gravity.CENTER_VERTICAL:
case Gravity.CENTER_VERTICAL | Gravity.LEFT:
mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
mFirstCenterY = paddingTop + contentHeight * 0.5f;
break;
default:
case Gravity.CENTER:
mFirstCenterX = paddingStart + contentWidth * 0.5f -
mItemCenterOffset * (count - 1) * 0.5f;
mFirstCenterY = paddingTop + contentHeight * 0.5f;
break;
case Gravity.CENTER_VERTICAL | Gravity.RIGHT:
mFirstCenterX = width - paddingEnd - itemWidth * scale * 0.5f -
mItemCenterOffset * (count - 1);
mFirstCenterY = paddingTop + contentHeight * 0.5f;
break;
case Gravity.BOTTOM:
case Gravity.BOTTOM | Gravity.LEFT:
mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
mFirstCenterY = height - paddingBottom - itemHeight * scale * 0.5f;
break;
case Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM:
mFirstCenterX = paddingStart + contentWidth * 0.5f -
mItemCenterOffset * (count - 1) * 0.5f;
mFirstCenterY = height - paddingBottom - itemHeight * scale * 0.5f;
break;
case Gravity.RIGHT | Gravity.BOTTOM:
mFirstCenterX = width - paddingEnd - itemWidth * scale * 0.5f -
mItemCenterOffset * (count - 1);
mFirstCenterY = height - paddingBottom - itemHeight * scale * 0.5f;
break;
// 水平方向上沾满
case Gravity.FILL:
mItemCenterOffset = count == 1 ? 0 :
(contentWidth - itemWidth * scale) / (count - 1);
mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
mFirstCenterY = paddingTop + contentHeight * 0.5f;
break;
case Gravity.FILL_HORIZONTAL:
case Gravity.FILL_HORIZONTAL | Gravity.TOP:
mItemCenterOffset = count == 1 ? 0 :
(contentWidth - itemWidth * scale) / (count - 1);
mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
mFirstCenterY = paddingTop + itemHeight * scale * 0.5f;
break;
case Gravity.FILL_HORIZONTAL | Gravity.BOTTOM:
mItemCenterOffset = count == 1 ? 0 :
(contentWidth - itemWidth * scale) / (count - 1);
mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
mFirstCenterY = height - paddingBottom - itemHeight * scale * 0.5f;
break;
}
}
public CommonLayoutParams() {
super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.FILL);
}