下面列出了androidx.annotation.DimenRes#androidx.core.view.MarginLayoutParamsCompat 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void resetChildMargins(int childIndex) {
if (getChildCount() == 0 || childIndex == -1) {
return;
}
MaterialButton currentButton = getChildButton(childIndex);
LayoutParams params = (LayoutParams) currentButton.getLayoutParams();
if (getOrientation() == VERTICAL) {
params.topMargin = 0;
params.bottomMargin = 0;
return;
}
MarginLayoutParamsCompat.setMarginEnd(params, 0);
MarginLayoutParamsCompat.setMarginStart(params, 0);
params.leftMargin = 0;
params.rightMargin = 0;
}
/**
* Whether the character counter functionality is enabled or not in this layout.
*
* @attr ref com.google.android.material.R.styleable#TextInputLayout_counterEnabled
*/
public void setCounterEnabled(boolean enabled) {
if (counterEnabled != enabled) {
if (enabled) {
counterView = new AppCompatTextView(getContext());
counterView.setId(R.id.textinput_counter);
if (typeface != null) {
counterView.setTypeface(typeface);
}
counterView.setMaxLines(1);
indicatorViewController.addIndicator(counterView, COUNTER_INDEX);
MarginLayoutParamsCompat.setMarginStart(
(MarginLayoutParams) counterView.getLayoutParams(),
getResources().getDimensionPixelOffset(R.dimen.mtrl_textinput_counter_margin_start));
updateCounterTextAppearanceAndColor();
updateCounter();
} else {
indicatorViewController.removeIndicator(counterView, COUNTER_INDEX);
counterView = null;
}
counterEnabled = enabled;
}
}
public void setTabButtonVisible(boolean visible) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
mSendText.getLayoutParams();
if (visible) {
MarginLayoutParamsCompat.setMarginStart(layoutParams, 0);
mTabIcon.setVisibility(View.VISIBLE);
} else {
MarginLayoutParamsCompat.setMarginStart(layoutParams, mContext.getResources()
.getDimensionPixelSize(R.dimen.message_edit_text_margin_left));
mTabIcon.setVisibility(View.GONE);
}
mSendText.setLayoutParams(layoutParams);
}
@Override
public void customizeRadioButton(AppCompatRadioButton button) {
button.setText(contentDescription);
LinearLayout.LayoutParams size =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
MarginLayoutParamsCompat.setMarginEnd(
size, button.getResources().getDimensionPixelSize(R.dimen.theme_switcher_radio_spacing));
button.setLayoutParams(size);
}
/**
* Sets a negative marginStart on all but the first child, if two adjacent children both have a
* stroke width greater than 0. This prevents a double-width stroke from being drawn for two
* adjacent stroked children, and instead draws the adjacent strokes directly on top of each
* other.
*
* <p>The negative margin adjustment amount will be equal to the smaller of the two adjacent
* stroke widths.
*
* <p>Also rearranges children such that they are shown in the correct visual order.
*/
private void adjustChildMarginsAndUpdateLayout() {
int firstVisibleChildIndex = getFirstVisibleChildIndex();
if (firstVisibleChildIndex == -1) {
return;
}
for (int i = firstVisibleChildIndex + 1; i < getChildCount(); i++) {
// Only adjusts margins if both adjacent children are MaterialButtons
MaterialButton currentButton = getChildButton(i);
MaterialButton previousButton = getChildButton(i - 1);
// Calculates the margin adjustment to be the smaller of the two adjacent stroke widths
int smallestStrokeWidth =
Math.min(currentButton.getStrokeWidth(), previousButton.getStrokeWidth());
LayoutParams params = buildLayoutParams(currentButton);
if (getOrientation() == HORIZONTAL) {
MarginLayoutParamsCompat.setMarginEnd(params, 0);
MarginLayoutParamsCompat.setMarginStart(params, -smallestStrokeWidth);
} else {
params.bottomMargin = 0;
params.topMargin = -smallestStrokeWidth;
}
currentButton.setLayoutParams(params);
}
resetChildMargins(firstVisibleChildIndex);
}
static int getMarginStart(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp);
}
static int getMarginEnd(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginEnd(lp);
}
static int getMarginHorizontally(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp) + MarginLayoutParamsCompat.getMarginEnd(lp);
}
private void setMarginStart(View view, @DimenRes int marginResId) {
int margin = getResources().getDimensionPixelOffset(marginResId);
MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
MarginLayoutParamsCompat.setMarginStart(layoutParams, margin);
}
@Override
protected void onLayout(boolean sizeChanged, int left, int top, int right, int bottom) {
if (getChildCount() == 0) {
// Do not re-layout when there are no children.
rowCount = 0;
return;
}
rowCount = 1;
boolean isRtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
int paddingStart = isRtl ? getPaddingRight() : getPaddingLeft();
int paddingEnd = isRtl ? getPaddingLeft() : getPaddingRight();
int childStart = paddingStart;
int childTop = getPaddingTop();
int childBottom = childTop;
int childEnd;
final int maxChildEnd = right - left - paddingEnd;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child.getVisibility() == View.GONE) {
child.setTag(R.id.row_index_key, -1);
continue;
}
LayoutParams lp = child.getLayoutParams();
int startMargin = 0;
int endMargin = 0;
if (lp instanceof MarginLayoutParams) {
MarginLayoutParams marginLp = (MarginLayoutParams) lp;
startMargin = MarginLayoutParamsCompat.getMarginStart(marginLp);
endMargin = MarginLayoutParamsCompat.getMarginEnd(marginLp);
}
childEnd = childStart + startMargin + child.getMeasuredWidth();
if (!singleLine && (childEnd > maxChildEnd)) {
childStart = paddingStart;
childTop = childBottom + lineSpacing;
rowCount++;
}
child.setTag(R.id.row_index_key, rowCount - 1);
childEnd = childStart + startMargin + child.getMeasuredWidth();
childBottom = childTop + child.getMeasuredHeight();
if (isRtl) {
child.layout(
maxChildEnd - childEnd, childTop, maxChildEnd - childStart - startMargin, childBottom);
} else {
child.layout(childStart + startMargin, childTop, childEnd, childBottom);
}
childStart += (startMargin + endMargin + child.getMeasuredWidth()) + itemSpacing;
}
}
private void updateTextAndIcon(
@Nullable final TextView textView, @Nullable final ImageView iconView) {
final Drawable icon =
(tab != null && tab.getIcon() != null)
? DrawableCompat.wrap(tab.getIcon()).mutate()
: null;
final CharSequence text = tab != null ? tab.getText() : null;
if (iconView != null) {
if (icon != null) {
iconView.setImageDrawable(icon);
iconView.setVisibility(VISIBLE);
setVisibility(VISIBLE);
} else {
iconView.setVisibility(GONE);
iconView.setImageDrawable(null);
}
}
final boolean hasText = !TextUtils.isEmpty(text);
if (textView != null) {
if (hasText) {
textView.setText(text);
if (tab.labelVisibilityMode == TAB_LABEL_VISIBILITY_LABELED) {
textView.setVisibility(VISIBLE);
} else {
textView.setVisibility(GONE);
}
setVisibility(VISIBLE);
} else {
textView.setVisibility(GONE);
textView.setText(null);
}
}
if (iconView != null) {
MarginLayoutParams lp = ((MarginLayoutParams) iconView.getLayoutParams());
int iconMargin = 0;
if (hasText && iconView.getVisibility() == VISIBLE) {
// If we're showing both text and icon, add some margin bottom to the icon
iconMargin = (int) ViewUtils.dpToPx(getContext(), DEFAULT_GAP_TEXT_ICON);
}
if (inlineLabel) {
if (iconMargin != MarginLayoutParamsCompat.getMarginEnd(lp)) {
MarginLayoutParamsCompat.setMarginEnd(lp, iconMargin);
lp.bottomMargin = 0;
// Calls resolveLayoutParams(), necessary for layout direction
iconView.setLayoutParams(lp);
iconView.requestLayout();
}
} else {
if (iconMargin != lp.bottomMargin) {
lp.bottomMargin = iconMargin;
MarginLayoutParamsCompat.setMarginEnd(lp, 0);
// Calls resolveLayoutParams(), necessary for layout direction
iconView.setLayoutParams(lp);
iconView.requestLayout();
}
}
}
final CharSequence contentDesc = tab != null ? tab.contentDesc : null;
TooltipCompat.setTooltipText(this, hasText ? null : contentDesc);
}