下面列出了android.view.View#getPaddingTop ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Construct the final url with the dimensions included as the last transformation in the url.
* @param view The view to adapt the image size to.
* @param baseUrl The base cloudinary Url to chain the transformation to.
* @return The url with the responsive transformation.
*/
private Url buildUrl(View view, Url baseUrl) {
// add a new transformation on top of anything already there:
Url url = baseUrl.clone();
url.transformation().chain();
if (autoHeight) {
int contentHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingBottom();
url.transformation().height(trimAndRoundUp(contentHeight));
}
if (autoWidth) {
int contentWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
url.transformation().width(trimAndRoundUp(contentWidth));
}
url.transformation().crop(cropMode).gravity(gravity);
return url;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int totalHeight;
if (getParent() instanceof View) {
View view = (View) getParent();
totalHeight = view.getMeasuredHeight();
if (view.getPaddingTop() != 0 && Build.VERSION.SDK_INT >= 21) {
totalHeight -= AndroidUtilities.statusBarHeight;
}
} else {
totalHeight = MeasureSpec.getSize(heightMeasureSpec);
}
if (totalHeight == 0) {
totalHeight = AndroidUtilities.displaySize.y - ActionBar.getCurrentActionBarHeight() - (Build.VERSION.SDK_INT >= 21 ? AndroidUtilities.statusBarHeight : 0);
}
if (currentType == 0 || currentType == 2 || currentType == 3) {
ArrayList<TLRPC.RecentMeUrl> arrayList = MessagesController.getInstance(currentAccount).hintDialogs;
if (!arrayList.isEmpty()) {
totalHeight -= AndroidUtilities.dp(72) * arrayList.size() + arrayList.size() - 1 + AndroidUtilities.dp(12 + 38);
}
super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(totalHeight, MeasureSpec.EXACTLY));
} else {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(166), MeasureSpec.EXACTLY));
}
}
@Override
public void prepare(View target) {
float x = target.getPaddingLeft();
float y = target.getPaddingTop();
getAnimatorAgent().playTogether(
Glider.glide(Skill.SineEaseInOut, 1300, ObjectAnimator.ofFloat(target, "rotation", 0, 80, 60, 80, 60, 60)),
ObjectAnimator.ofFloat(target, "translationY", 0, 0, 0, 0, 0, 700),
ObjectAnimator.ofFloat(target, "alpha", 1, 1, 1, 1, 1, 0),
ObjectAnimator.ofFloat(target, "pivotX", x, x, x, x, x, x),
ObjectAnimator.ofFloat(target, "pivotY", y, y, y, y, y, y)
);
setDuration(1300);
}
@Override
public void showMoreKeysPanel(final View parentView, final Controller controller,
final int pointX, final int pointY, final KeyboardActionListener listener) {
mController = controller;
mListener = listener;
final View container = getContainerView();
// The coordinates of panel's left-top corner in parentView's coordinate system.
// We need to consider background drawable paddings.
final int x = pointX - getDefaultCoordX() - container.getPaddingLeft() - getPaddingLeft();
final int y = pointY - container.getMeasuredHeight() + container.getPaddingBottom()
+ getPaddingBottom();
parentView.getLocationInWindow(mCoordinates);
// Ensure the horizontal position of the panel does not extend past the parentView edges.
final int maxX = parentView.getMeasuredWidth() - container.getMeasuredWidth();
final int panelX = Math.max(0, Math.min(maxX, x)) + CoordinateUtils.x(mCoordinates);
final int panelY = y + CoordinateUtils.y(mCoordinates);
container.setX(panelX);
container.setY(panelY);
mOriginX = x + container.getPaddingLeft();
mOriginY = y + container.getPaddingTop();
controller.onShowMoreKeysPanel(this);
final MoreKeysKeyboardAccessibilityDelegate accessibilityDelegate = mAccessibilityDelegate;
if (accessibilityDelegate != null
&& AccessibilityUtils.Companion.getInstance().isAccessibilityEnabled()) {
accessibilityDelegate.onShowMoreKeysKeyboard();
}
}
@Override
protected void execute(View view, int val)
{
int l = view.getPaddingLeft();
int t = view.getPaddingTop();
int r = view.getPaddingRight();
int b = val;
view.setPadding(l, t, r, b);
}
public static boolean canScrollLeft(@NonNull View targetView) {
if (android.os.Build.VERSION.SDK_INT < 14) {
if (targetView instanceof AbsListView) {
final ViewGroup viewGroup = (ViewGroup) targetView;
final AbsListView absListView = (AbsListView) targetView;
return viewGroup.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0
|| viewGroup.getChildAt(0).getTop() < targetView.getPaddingTop());
} else {
return targetView.getScrollY() > 0;
}
} else {
return targetView.canScrollHorizontally(-1);
}
}
public static void autoPadding(View view){
int l = view.getPaddingLeft();
int t = view.getPaddingTop();
int r = view.getPaddingRight();
int b = view.getPaddingBottom();
l = getDisplayWidthValue(l);
t = getDisplayHeightValue(t);
r = getDisplayWidthValue(r);
b = getDisplayHeightValue(b);
view.setPadding(l, t, r, b);
}
/**
* Apply window insets padding for the supplied view.
*
* @param view The view to set the insets padding.
* @param left {@code true} to apply the left window inset padding.
* @param top {@code true} to apply the top window inset padding.
* @param right {@code true} to apply the right window inset padding.
* @param bottom {@code true} to apply the bottom window inset padding.
* @param consume {@code true} to consume the applied window insets.
*/
public static void applyWindowInsets(@Nullable View view, final boolean left,
final boolean top, final boolean right, final boolean bottom, final boolean consume) {
if (view == null) {
return;
}
final int paddingLeft = view.getPaddingLeft();
final int paddingTop = view.getPaddingTop();
final int paddingRight = view.getPaddingRight();
final int paddingBottom = view.getPaddingBottom();
ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
v.setPadding(left ? paddingLeft + insets.getSystemWindowInsetLeft(): paddingLeft,
top ? paddingTop + insets.getSystemWindowInsetTop() : paddingTop,
right ? paddingRight + insets.getSystemWindowInsetRight() : paddingRight,
bottom ? paddingBottom + insets.getSystemWindowInsetBottom() : paddingBottom);
return !consume ? insets :
new WindowInsetsCompat.Builder(insets).setSystemWindowInsets(
Insets.of(left ? 0 : insets.getSystemWindowInsetLeft(),
top ? 0 : insets.getSystemWindowInsetTop(),
right ? 0 : insets.getSystemWindowInsetRight(),
bottom ? 0 : insets.getSystemWindowInsetBottom()))
.build();
}
});
requestApplyWindowInsets(view);
}
@Override
protected void execute(View view, int val) {
int l = val;
int t = view.getPaddingTop();
int r = view.getPaddingRight();
int b = view.getPaddingBottom();
view.setPadding(l, t, r, b);
}
@Override
public boolean onTouch(final View v, final MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN && drawable != null) {
final int x = (int) event.getX();
final int y = (int) event.getY();
final Rect bounds = drawable.getBounds();
if (x >= (v.getRight() - bounds.width() - fuzz) && x <= (v.getRight() - v.getPaddingRight() + fuzz)
&& y >= (v.getPaddingTop() - fuzz) && y <= (v.getHeight() - v.getPaddingBottom()) + fuzz) {
return onDrawableTouch(event);
}
}
return false;
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
if (mDrawDivider) {
int left = view.getPaddingLeft();
int right = view.getPaddingRight();
int top = view.getPaddingTop();
int bottom = view.getPaddingBottom();
view.setBackground(DividerDrawable.create(getContext()));
view.setPadding(left, top, right, bottom);
}
SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.switch_widget);
// On BLU Life Play devices SwitchPreference.setWidgetLayoutResource() does nothing. As a
// result, the user will see a non-material Switch and switchView will be null, hence the
// null check below. http://crbug.com/451447
if (switchView != null) {
switchView.setChecked(isChecked());
}
TextView title = (TextView) view.findViewById(android.R.id.title);
title.setSingleLine(false);
if (!mDontUseSummaryAsTitle && TextUtils.isEmpty(getTitle())) {
TextView summary = (TextView) view.findViewById(android.R.id.summary);
title.setText(summary.getText());
title.setVisibility(View.VISIBLE);
summary.setVisibility(View.GONE);
}
if (mManagedPrefDelegate != null) mManagedPrefDelegate.onBindViewToPreference(this, view);
}
public void updateLayout() {
if (getParent() instanceof View && (currentType == 2 || currentType == 3)) {
View view = (View) getParent();
int paddingTop = view.getPaddingTop();
if (paddingTop != 0) {
int offset = -(getTop() / 2);
imageView.setTranslationY(offset);
emptyTextView1.setTranslationY(offset);
emptyTextView2.setTranslationY(offset);
}
}
}
/**
* 获取 View Padding
* @param view {@link View}
* @return new int[] {left, top, right, bottom}
*/
public static int[] getPadding(final View view) {
int[] paddings = new int[]{0, 0, 0, 0};
if (view != null) {
paddings[0] = view.getPaddingLeft();
paddings[1] = view.getPaddingTop();
paddings[2] = view.getPaddingRight();
paddings[3] = view.getPaddingBottom();
}
return paddings;
}
/**
* CardView adds extra padding on pre-lollipop devices for shadows
* This function removes that extra padding from its margins
*
* @param cardView The CardView that needs adjustments
* @return float
*/
public static void adjustCardViewMargins(View cardView) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) cardView.getLayoutParams();
params.topMargin -= cardView.getPaddingTop();
params.leftMargin -= cardView.getPaddingLeft();
params.rightMargin -= cardView.getPaddingRight();
cardView.setLayoutParams(params);
}
@Override
protected void execute(View view, int val)
{
int l = view.getPaddingLeft();
int t = view.getPaddingTop();
int r = val;
int b = view.getPaddingBottom();
view.setPadding(l, t, r, b);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), heightMeasureSpec);
if (isLast) {
View parent = (View) getParent();
if (parent != null) {
int height = parent.getMeasuredHeight() - parent.getPaddingBottom() - parent.getPaddingTop() - AndroidUtilities.dp(24);
if (getMeasuredHeight() < height) {
setMeasuredDimension(getMeasuredWidth(), height);
}
}
}
}
private void setAmPmStart(boolean isAmPmAtStart) {
final RelativeLayout.LayoutParams params =
(RelativeLayout.LayoutParams) mAmPmLayout.getLayoutParams();
if (params.getRule(RelativeLayout.RIGHT_OF) != 0
|| params.getRule(RelativeLayout.LEFT_OF) != 0) {
// Horizontal mode, with AM/PM appearing to left/right of hours and minutes.
final boolean isAmPmAtLeft;
if (TextUtils.getLayoutDirectionFromLocale(mLocale) == View.LAYOUT_DIRECTION_LTR) {
isAmPmAtLeft = isAmPmAtStart;
} else {
isAmPmAtLeft = !isAmPmAtStart;
}
if (mIsAmPmAtLeft == isAmPmAtLeft) {
// AM/PM is already at the correct location. No change needed.
return;
}
if (isAmPmAtLeft) {
params.removeRule(RelativeLayout.RIGHT_OF);
params.addRule(RelativeLayout.LEFT_OF, mHourView.getId());
} else {
params.removeRule(RelativeLayout.LEFT_OF);
params.addRule(RelativeLayout.RIGHT_OF, mMinuteView.getId());
}
mIsAmPmAtLeft = isAmPmAtLeft;
} else if (params.getRule(RelativeLayout.BELOW) != 0
|| params.getRule(RelativeLayout.ABOVE) != 0) {
// Vertical mode, with AM/PM appearing to top/bottom of hours and minutes.
if (mIsAmPmAtTop == isAmPmAtStart) {
// AM/PM is already at the correct location. No change needed.
return;
}
final int otherViewId;
if (isAmPmAtStart) {
otherViewId = params.getRule(RelativeLayout.BELOW);
params.removeRule(RelativeLayout.BELOW);
params.addRule(RelativeLayout.ABOVE, otherViewId);
} else {
otherViewId = params.getRule(RelativeLayout.ABOVE);
params.removeRule(RelativeLayout.ABOVE);
params.addRule(RelativeLayout.BELOW, otherViewId);
}
// Switch the top and bottom paddings on the other view.
final View otherView = mRadialTimePickerHeader.findViewById(otherViewId);
final int top = otherView.getPaddingTop();
final int bottom = otherView.getPaddingBottom();
final int left = otherView.getPaddingLeft();
final int right = otherView.getPaddingRight();
otherView.setPadding(left, bottom, right, top);
mIsAmPmAtTop = isAmPmAtStart;
}
mAmPmLayout.setLayoutParams(params);
}
/**
* Update the backgrounds for the buttons to account for their current positioning.
* The primary and secondary buttons are special-cased in that their backgrounds change to
* create the illusion of a single-stroke boundary between them.
*/
private void updateBackgroundsForButtons() {
boolean bothButtonsExist = findViewById(R.id.button_primary) != null
&& findViewById(R.id.button_secondary) != null;
for (int row = 0; row < mIndicesOfRows.size() - 1; row++) {
final int rowStart = mIndicesOfRows.get(row);
final int rowEnd = mIndicesOfRows.get(row + 1);
final int rowSize = rowEnd - rowStart;
for (int i = rowStart; i < rowEnd; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == View.GONE || !isButton(child)) continue;
// Determine which background we need to show.
int background;
if (row == 0) {
// Button will be floating.
background = mBackgroundFloating;
} else if (rowSize == 1 || !bothButtonsExist) {
// Button takes up the full width of the screen.
background = mBackgroundFullRight;
} else if (mLayoutRTL) {
// Primary button will be to the left of the secondary.
background = child.getId() == R.id.button_primary
? mBackgroundFullLeft : mBackgroundFullRight;
} else {
// Primary button will be to the right of the secondary.
background = child.getId() == R.id.button_primary
? mBackgroundFullRight : mBackgroundFullLeft;
}
// Update the background.
LayoutParams params = (LayoutParams) child.getLayoutParams();
if (params.background != background) {
params.background = background;
// Save the padding; Android decides to overwrite it on some builds.
int paddingLeft = child.getPaddingLeft();
int paddingTop = child.getPaddingTop();
int paddingRight = child.getPaddingRight();
int paddingBottom = child.getPaddingBottom();
int buttonWidth = child.getMeasuredWidth();
int buttonHeight = child.getMeasuredHeight();
// Set the background, then restore the padding.
child.setBackgroundResource(background);
child.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
// Re-measuring is necessary to correct the text gravity.
int specWidth = MeasureSpec.makeMeasureSpec(buttonWidth, MeasureSpec.EXACTLY);
int specHeight = MeasureSpec.makeMeasureSpec(buttonHeight, MeasureSpec.EXACTLY);
measureChild(child, specWidth, specHeight);
}
}
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int childCount = getChildCount();
final int parentLeft = getPaddingLeft();
final int parentTop = getPaddingTop();
final int parentRight = r - l - getPaddingRight();
final int parentBottom = b - t - getPaddingBottom();
final int parentWidth = parentRight - parentLeft;
final int parentHeight = parentBottom - parentTop;
int unpaddedWidth, unpaddedHeight, parentUnpaddedWidth, parentUnpaddedHeight;
int childPaddingLeft, childPaddingTop, childPaddingRight, childPaddingBottom;
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
// Fit and center the child within the parent. Make sure not to consider padding
// as part of the child's aspect ratio.
childPaddingLeft = child.getPaddingLeft();
childPaddingTop = child.getPaddingTop();
childPaddingRight = child.getPaddingRight();
childPaddingBottom = child.getPaddingBottom();
unpaddedWidth = child.getMeasuredWidth() - childPaddingLeft - childPaddingRight;
unpaddedHeight = child.getMeasuredHeight() - childPaddingTop - childPaddingBottom;
parentUnpaddedWidth = parentWidth - childPaddingLeft - childPaddingRight;
parentUnpaddedHeight = parentHeight - childPaddingTop - childPaddingBottom;
if (parentUnpaddedWidth * unpaddedHeight > parentUnpaddedHeight * unpaddedWidth) {
// The child view should be left/right letterboxed.
final int scaledChildWidth = unpaddedWidth * parentUnpaddedHeight
/ unpaddedHeight + childPaddingLeft + childPaddingRight;
child.layout(
parentLeft + (parentWidth - scaledChildWidth) / 2,
parentTop,
parentRight - (parentWidth - scaledChildWidth) / 2,
parentBottom);
} else {
// The child view should be top/bottom letterboxed.
final int scaledChildHeight = unpaddedHeight * parentUnpaddedWidth
/ unpaddedWidth + childPaddingTop + childPaddingBottom;
child.layout(
parentLeft,
parentTop + (parentHeight - scaledChildHeight) / 2,
parentRight,
parentTop + (parentHeight + scaledChildHeight) / 2);
}
}
}
private String getViewExtraInfo(View v) {
StringBuilder info = new StringBuilder();
// 背景色
Drawable drawable = v.getBackground();
if (drawable != null) {
if (drawable instanceof ColorDrawable) {
int colorInt = ((ColorDrawable) drawable).getColor();
String backgroundColor = ColorUtil.parseColorInt(colorInt);
info.append(getResources().getString(R.string.dk_view_check_info_desc, backgroundColor));
info.append("\n");
}
}
// padding
if (v.getPaddingLeft() != 0 && v.getPaddingTop() != 0 && v.getPaddingRight() != 0 && v.getPaddingBottom() != 0) {
info.append(getResources().getString(R.string.dk_view_check_info_padding, v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), v.getPaddingBottom()));
info.append("\n");
}
// margin
final ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
final ViewGroup.MarginLayoutParams mp = ((ViewGroup.MarginLayoutParams) layoutParams);
if (mp.leftMargin != 0 && mp.topMargin != 0 && mp.rightMargin != 0 && mp.bottomMargin != 0) {
info.append(getResources().getString(R.string.dk_view_check_info_margin, mp.leftMargin, mp.topMargin, mp.rightMargin, mp.bottomMargin));
info.append("\n");
}
}
// TextView信息
if (v instanceof TextView) {
TextView tv = ((TextView) v);
String textColor = ColorUtil.parseColorInt(tv.getCurrentTextColor());
info.append(getResources().getString(R.string.dk_view_check_info_text_color, textColor));
info.append("\n");
info.append(getResources().getString(R.string.dk_view_check_info_text_size, (int) tv.getTextSize()));
info.append("\n");
}
// 删除最后一个换行
if (!TextUtils.isEmpty(info)) {
info.deleteCharAt(info.length() - 1);
}
return info.toString();
}