下面列出了android.view.View#requestLayout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void setChildHeightForHorizontalLayout(View view, int height) {
// In a horizontal layout, if a child's height is set to fill parent, we can simply set the
// LayoutParams height to fill parent.
Object layoutParams = view.getLayoutParams();
if (layoutParams instanceof LinearLayout.LayoutParams) {
LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) layoutParams;
switch (height) {
case Component.LENGTH_PREFERRED:
linearLayoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
break;
case Component.LENGTH_FILL_PARENT:
linearLayoutParams.height = LinearLayout.LayoutParams.FILL_PARENT;
break;
default:
linearLayoutParams.height = calculatePixels(view, height);
break;
}
view.requestLayout();
} else {
Log.e("ViewUtil", "The view does not have linear layout parameters");
}
}
public void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
private static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
/**
* 关闭缩放
*/
public synchronized void stopScaleModel() {
if (!isInScaleMode) {
return;
}
isInScaleMode = false;
restoreView(contentView);
restoreView(horizontalView);
for (View view : verticalViewList) {
restoreView(view);
view.requestLayout();
}
horizontalView.setScaleX(1f);
horizontalView.setScaleY(1f);
}
@UiThread
private void collapse(@NonNull final View view) {
isOpened = false;
final int initialHeight = view.getMeasuredHeight();
final Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
view.setVisibility(View.GONE);
isOpened = false;
} else {
view.getLayoutParams().height = initialHeight -
(int) (initialHeight * interpolatedTime);
view.requestLayout();
}
}
};
animation.setDuration(duration);
view.startAnimation(animation);
}
private static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
private static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
public static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration(500);
v.startAnimation(a);
}
public static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
private void blueStreakOfProgress(Integer current, Integer max) {
// Set up the progress bar
if (mProgressWidth == 0) {
final View vShareProgressEmpty = (View) findViewById(R.id.vShareProgressEmpty);
mProgressWidth = vShareProgressEmpty.getMeasuredWidth();
}
// Display stuff
if (max == 0)
max = 1;
int width = (int) ((float) mProgressWidth) * current / max;
View vShareProgressFull = (View) findViewById(R.id.vShareProgressFull);
vShareProgressFull.getLayoutParams().width = width;
vShareProgressFull.invalidate();
vShareProgressFull.requestLayout();
}
public static void setMargins(View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
params.setMargins(left, top, right, bottom);
view.requestLayout();
}
}
private void applyMarginToFullChildViews(ViewGroup contentView, float margin) {
for (int i = 0; i < contentView.getChildCount(); i++) {
View child = contentView.getChildAt(i);
if (!(child.getLayoutParams() instanceof FrameLayout.LayoutParams)) continue;
FrameLayout.LayoutParams layoutParams =
(FrameLayout.LayoutParams) child.getLayoutParams();
if (layoutParams.height == LayoutParams.MATCH_PARENT
&& layoutParams.topMargin != (int) margin) {
layoutParams.topMargin = (int) margin;
child.requestLayout();
TraceEvent.instant("FullscreenManager:child.requestLayout()");
}
}
}
/**
* 兼容全面屏的状态栏高度
*/
public void setMargins(View view, int l, int t, int r, int b) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(l, t, r, b);
view.requestLayout();
}
}
private void requestLayoutChildren() {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View v = getChildAt(i);
if (v != null) v.requestLayout();
}
}
private void calculateScrolledPosition(int totalItemCount, RecyclerView recyclerView) {
for (int j = 0; j < totalItemCount - 1; j++) {
View view = recyclerView.getChildAt(j);
if (view != null) {
if (!(recyclerView.getChildViewHolder(view) instanceof FocusResizeAdapter.FooterViewHolder)) {
if (j == itemToResize) {
onItemBigResize(view, recyclerView);
} else {
onItemSmallResize(view, recyclerView);
}
view.requestLayout();
}
}
}
}
public static void setSize(@NonNull View view, int width, int height) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
lp.height = height;
lp.width = width;
view.requestLayout();
}
private void setWeight(int id, float progress) {
View before = findViewById(id);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) before.getLayoutParams();
layoutParams.weight = progress;
before.requestLayout();
}
/**
* 设置view margin
*
* @param v
* @param l
* @param t
* @param r
* @param b
*/
public static void setMargins(View v, int l, int t, int r, int b) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(l, t, r, b);
v.requestLayout();
}
}
/**
* 设置View的外边距(Margins)
*
* @param view 要设置外边距的View
* @param left 左侧外边距
* @param top 顶部外边距
* @param right 右侧外边距
* @param bottom 底部外边距
*/
public static void setMargins(View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(left, top, right, bottom);
view.requestLayout(); //请求重绘
}
}
/**
* Allows to set the {@link ViewProgressBinding#progressStreakLeft} or {@link ViewProgressBinding#progressStreakRight} height accordingly to the CircularBar dimension.
*
* @param streakHeight The streak we want to set the dimension to
*/
public void setStreakHeight(View streakHeight) {
streakHeight.getLayoutParams().height = (int) mBinding.circularBar.getDiameter();
streakHeight.requestLayout();
}