下面列出了android.view.View#setPadding ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public SmartViewHolder(View itemView, AdapterView.OnItemClickListener mListener) {
super(itemView);
this.mListener = mListener;
itemView.setOnClickListener(this);
/*
* 设置水波纹背景
*/
if (itemView.getBackground() == null) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = itemView.getContext().getTheme();
int top = itemView.getPaddingTop();
int bottom = itemView.getPaddingBottom();
int left = itemView.getPaddingLeft();
int right = itemView.getPaddingRight();
if (theme.resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true)) {
itemView.setBackgroundResource(typedValue.resourceId);
}
itemView.setPadding(left, top, right, bottom);
}
}
@Keep
public void setTopPadding(float value) {
topPadding = value;
if (fragment != null && getParent() != null) {
View view = fragment.getFragmentView();
int additionalPadding = 0;
if (additionalContextView != null && additionalContextView.getVisibility() == VISIBLE && additionalContextView.getParent() != null) {
additionalPadding = AndroidUtilities.dp(36);
}
if (view != null && getParent() != null) {
view.setPadding(0, (int) topPadding + additionalPadding, 0, 0);
}
if (isLocation && additionalContextView != null) {
((LayoutParams) additionalContextView.getLayoutParams()).topMargin = -AndroidUtilities.dp(36) - (int) topPadding;
}
}
}
public static void autoPadding(View view)
{
Object tag = view.getTag(R.id.id_tag_autolayout_padding);
if (tag != null) return;
view.setTag(R.id.id_tag_autolayout_padding, "Just Identify");
int l = view.getPaddingLeft();
int t = view.getPaddingTop();
int r = view.getPaddingRight();
int b = view.getPaddingBottom();
l = getPercentWidthSize(l);
t = getPercentHeightSize(t);
r = getPercentWidthSize(r);
b = getPercentHeightSize(b);
view.setPadding(l, t, r, b);
}
/**
* Sets the background for a view while preserving its current padding. If the background drawable
* has its own padding, that padding will be added to the current padding.
*
* @param view View to receive the new background.
* @param backgroundDrawable Drawable to set as new background.
*/
public static void setBackgroundAndKeepPadding(View view, Drawable backgroundDrawable) {
Rect drawablePadding = new Rect();
backgroundDrawable.getPadding(drawablePadding);
int top = view.getPaddingTop() + drawablePadding.top;
int left = view.getPaddingLeft() + drawablePadding.left;
int right = view.getPaddingRight() + drawablePadding.right;
int bottom = view.getPaddingBottom() + drawablePadding.bottom;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
view.setBackgroundDrawable(backgroundDrawable);
} else {
view.setBackground(backgroundDrawable);
}
view.setPadding(left, top, right, bottom);
}
public void measureChild(View child) {
CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
if (!lp.isFullscreen) {
final DeviceProfile profile = mLauncher.getDeviceProfile();
if (child instanceof LauncherAppWidgetHostView) {
lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX,
profile.appWidgetScale.x, profile.appWidgetScale.y);
// Widgets have their own padding
} else {
lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX);
// Center the icon/folder
int cHeight = getCellContentHeight();
int cellPaddingY = (int) Math.max(0, ((lp.height - cHeight) / 2f));
int cellPaddingX = (int) (profile.edgeMarginPx / 2f);
child.setPadding(cellPaddingX, cellPaddingY, cellPaddingX, 0);
}
} else {
lp.x = 0;
lp.y = 0;
lp.width = getMeasuredWidth();
lp.height = getMeasuredHeight();
}
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
child.measure(childWidthMeasureSpec, childheightMeasureSpec);
}
/** 增加View的paddingTop,增加的值为状态栏高度 (智能判断,并设置高度)*/
public static void setPaddingSmart(Context context, View view) {
if (Build.VERSION.SDK_INT >= MIN_API) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp != null && lp.height > 0) {
lp.height += getStatusBarHeight(context);//增高
}
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context),
view.getPaddingRight(), view.getPaddingBottom());
}
}
/**
* 添加SnackBar视图
* <p>在{@link #show()}之后调用</p>
*
* @param layoutId 布局文件
* @param params 布局参数
*/
public static void addView(@LayoutRes final int layoutId, @NonNull final ViewGroup.LayoutParams params) {
final View view = getView();
if (view != null) {
view.setPadding(0, 0, 0, 0);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) view;
View child = LayoutInflater.from(view.getContext()).inflate(layoutId, null);
layout.addView(child, -1, params);
}
}
/** 增加View的高度以及paddingTop,增加的值为状态栏高度.一般是在沉浸式全屏给ToolBar用的 */
public static void setHeightAndPadding(Context context, View view) {
if (Build.VERSION.SDK_INT >= MIN_API) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
lp.height += getStatusBarHeight(context);//增高
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context),
view.getPaddingRight(), view.getPaddingBottom());
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
int horizontalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
int verticalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
int topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, getResources().getDisplayMetrics());
if (view != null) {
view.setPadding(horizontalMargin, topMargin, horizontalMargin, verticalMargin);
}
return view;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
int horizontalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
int verticalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
int topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, getResources().getDisplayMetrics());
if (view != null) {
view.setPadding(horizontalMargin, topMargin, horizontalMargin, verticalMargin);
}
return view;
}
@Override
protected void onBindView(final View view) {
super.onBindView(view);
// On pre-L devices, PreferenceCategoryWithButtonStyle is reused for PreferenceCategory,
// which needs a top padding of 16dp; we don't want this top padding for
// PreferenceCategoryWithButton views.
view.setPadding(view.getPaddingLeft(), 0, view.getPaddingRight(), view.getPaddingBottom());
View button = view.findViewById(android.R.id.icon);
button.setOnClickListener(this);
if (!TextUtils.isEmpty(mContentDescription)) {
button.setContentDescription(mContentDescription);
}
}
private static void updateTopBottomPadding(View view, int topPadding, int bottomPadding) {
if (ViewCompat.isPaddingRelative(view)) {
ViewCompat.setPaddingRelative(view,
ViewCompat.getPaddingStart(view), topPadding,
ViewCompat.getPaddingEnd(view), bottomPadding);
} else {
view.setPadding(view.getPaddingLeft(), topPadding,
view.getPaddingRight(), bottomPadding);
}
}
public static void setHeaderUserSelector(View headerCornerTab) {
if (headerCornerTab == null) {
return;
}
Theme theme = ThemeUtil.createTheme(headerCornerTab.getContext());
EditText etFilterName = (EditText)headerCornerTab.findViewById(R.id.etFilterName);
Button btnSearch = (Button)headerCornerTab.findViewById(R.id.btnSearch);
headerCornerTab.setBackgroundDrawable(theme.getDrawable("bg_header_corner_tab"));
int padding8 = theme.dip2px(8);
headerCornerTab.setPadding(padding8, padding8, padding8, padding8);
etFilterName.setBackgroundDrawable(theme.getDrawable("bg_input_frame_left_half"));
btnSearch.setBackgroundDrawable(theme.getDrawable("selector_btn_search"));
}
/**
* 增加View的paddingTop,增加的值为状态栏高度 (智能判断,并设置高度)
*/
public static void setPaddingSmart(Context context, View view) {
if (Build.VERSION.SDK_INT >= MIN_API) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp != null && lp.height > 0) {
lp.height += getStatusBarHeight(context);//增高
}
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context),
view.getPaddingRight(), view.getPaddingBottom());
}
}
public PDFLineSeparatorView(Context context) {
super(context);
View separatorLine = new View(context);
LinearLayout.LayoutParams separatorLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
separatorLine.setPadding(0, 5, 0, 5);
separatorLine.setLayoutParams(separatorLayoutParam);
super.setView(separatorLine);
}
public static void setPadding(View view, int left, int top, int right, int bottom) {
int scaledLeft = scaleValue(view.getContext(), left);
int scaledTop = scaleValue(view.getContext(), top);
int scaledRight = scaleValue(view.getContext(), right);
int scaledBottom = scaleValue(view.getContext(), bottom);
view.setPadding(scaledLeft, scaledTop, scaledRight, scaledBottom);
}
public static void appendStatusBarPadding(Context context, View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (view != null) {
int statusBarHeight = getStatusBarHeight(context);
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp != null && lp.height > 0) {
lp.height += statusBarHeight;//增高
}
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + statusBarHeight,
view.getPaddingRight(), view.getPaddingBottom());
}
}
}
static void hide(View view) {
view.setPadding(0, 0, 0, 0);
ViewGroup.LayoutParams lparam = view.getLayoutParams();
lparam.width = 0;
lparam.height = 0;
if (lparam instanceof ConstraintLayout.LayoutParams)
((ConstraintLayout.LayoutParams) lparam).setMargins(0, 0, 0, 0);
view.setLayoutParams(lparam);
}
@Override
public void apply(View root, ViewGroup rootParent, OnClickHandler handler) {
final View target = root.findViewById(viewId);
if (target == null) return;
target.setPadding(left, top, right, bottom);
}
/**
* Applies the dialog's bottom padding to the view of a specific area.
*
* @param area The area, the view, the padding should be applied to, corresponds to, as an instance
* of the class {@link Area}. The area may not be null
* @param view The view, the padding should be applied to, as an instance of the class {@link View}.
* The view may not be null
*/
private void applyDialogPaddingBottom(@NonNull final Area area, @NonNull final View view) {
if (area != Area.HEADER && area != Area.BUTTON_BAR) {
view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(),
dialogPadding[3]);
}
}