下面列出了android.view.View#getLayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Find the highest displayed edge of the section. If there is no member found then return the
* default edge instead.
*
* @param sectionFirstPosition Section id, position of first item in the section.
* @param firstIndex Child index to start looking from.
* @param defaultEdge Default value.
* @return Top (attached) edge of the section.
*/
public int getHighestEdge(int sectionFirstPosition, int firstIndex, int defaultEdge) {
// Look from start to find children that are the highest.
for (int i = firstIndex; i < mLayoutManager.getChildCount(); i++) {
View child = mLayoutManager.getChildAt(i);
LayoutManager.LayoutParams params = (LayoutManager.LayoutParams) child
.getLayoutParams();
if (params.getTestedFirstPosition() != sectionFirstPosition) {
break;
}
if (params.isHeader) {
continue;
}
// A more interesting layout would have to do something more here.
return mLayoutManager.getDecoratedTop(child);
}
return defaultEdge;
}
public void setPanelHeight(int height) {
panelHeight = height;
if (isFullSize && botButtons != null && botButtons.rows.size() != 0) {
buttonHeight = !isFullSize ? 42 : (int) Math.max(42, (panelHeight - AndroidUtilities.dp(30) - (botButtons.rows.size() - 1) * AndroidUtilities.dp(10)) / botButtons.rows.size() / AndroidUtilities.density);
int count = container.getChildCount();
int newHeight = AndroidUtilities.dp(buttonHeight);
for (int a = 0; a < count; a++) {
View v = container.getChildAt(a);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) v.getLayoutParams();
if (layoutParams.height != newHeight) {
layoutParams.height = newHeight;
v.setLayoutParams(layoutParams);
}
}
}
}
/**
* Close the specified drawer view by animating it into view.
*
* @param drawerView Drawer view to close
*/
public void closeDrawer(View drawerView) {
if (!isDrawerView(drawerView)) {
throw new IllegalArgumentException("View " + drawerView + " is not a sliding drawer");
}
if (mFirstLayout) {
final LayoutParams lp = (LayoutParams) drawerView.getLayoutParams();
lp.onScreen = 0.f;
lp.knownOpen = false;
} else {
if (checkDrawerViewAbsoluteGravity(drawerView, Gravity.LEFT)) {
mLeftDragger.smoothSlideViewTo(drawerView, -drawerView.getWidth(),
drawerView.getTop());
} else {
mRightDragger.smoothSlideViewTo(drawerView, getWidth(), drawerView.getTop());
}
}
invalidate();
}
/**
* wrap a view
* @param view the view to be wrapped
* @return the original wrapper or create a new wrapper to wrap the view and replace its place into parent
*/
public static SmartSwipeWrapper wrap(View view) {
SmartSwipeWrapper wrapper = peekWrapperFor(view);
if (wrapper != null) {
return wrapper;
}
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
if (view.getParent() != null) {
ViewGroup viewParent = (ViewGroup) view.getParent();
wrapper = createNewWrapper(view.getContext());
int index = viewParent.indexOfChild(view);
viewParent.removeView(view);
viewParent.addView(wrapper, index, layoutParams);
} else {
wrapper = createNewWrapper(view.getContext());
if (layoutParams != null) {
wrapper.setLayoutParams(layoutParams);
}
}
wrapper.setContentView(view);
return wrapper;
}
private void drawVertical(Canvas c, RecyclerView parent)
{
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++)
{
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDividerSize;
if (mDividerDrawable != null)
{
mDividerDrawable.setBounds(left, top, right, bottom);
mDividerDrawable.draw(c);
} else if (mDividerPaint != null)
{
c.drawRect(left, top, right, bottom, mDividerPaint);
}
}
}
private void inflate(Context context, View root, String text, Drawable drawable, int res,
int tv_id, int iv_id, int verb) {
if (!TextUtils.isEmpty(text)) {
TextView tv = (TextView) root.findViewById(tv_id);
if (tv != null) {
tv.setText(text);
tv.setVisibility(VISIBLE);
}
} else if (drawable != null) {
ImageView iv = (ImageView) root.findViewById(iv_id);
if (iv != null) {
iv.setImageDrawable(drawable);
iv.setVisibility(VISIBLE);
}
} else if (res != -1) {
View view = LayoutInflater.from(context).inflate(res, this, false);
addView(view);
LayoutParams lp = (LayoutParams) view.getLayoutParams();
lp.addRule(verb);
lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(lp);
}
}
/**
* 设置视图的宽高
*
* @param view
* @param width
* @param height
*/
public void setViewWidthAndHeight(View view, int width, int height) {
if (view == null) {
return;
}
ViewGroup.LayoutParams lp = view.getLayoutParams();
lp.width = width;
lp.height = height;
view.setLayoutParams(lp);
}
private void removeNonDecorViews() {
for (int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (!lp.isDecor) {
removeViewAt(i);
i--;
}
}
}
@Override
public void measureChildWithMargins(View child, int widthUsed, int heightUsed) {
RecyclerView.ViewHolder holder = listView.findContainingViewHolder(child);
int pos = holder.getAdapterPosition();
if (pos == getItemCount() - 1) {
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
layoutParams.height = Math.max(lastItemHeight, 0);
}
super.measureChildWithMargins(child, 0, 0);
}
/** 增加View的高度以及paddingTop,增加的值为状态栏高度.一般是在沉浸式全屏给ToolBar用的 */
public static void setHeightAndPadding(Context context, View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
lp.height += getStatusBarHeight(context);//增高
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context),
view.getPaddingRight(), view.getPaddingBottom());
}
}
@Override
public void addView(View child, int index) {
RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
if (showOneItemOnly) {
if (canScrollHorizontally()) {
lp.width = getWidth();
if (lp.height != -2) {
lp.height = getHeight();
}
} else {
lp.height = getHeight();
if (lp.width != -2) {
lp.width = getWidth();
}
}
} else {
if (lp instanceof LayoutParams) {
lp.width = ((LayoutParams) lp).origWidth;
lp.height = ((LayoutParams) lp).origHeight;
} else if (lp instanceof ScalableGridLayoutManager.LayoutParams) {
lp.width = ((ScalableGridLayoutManager.LayoutParams) lp).getOrigWidth();
lp.height = ((ScalableGridLayoutManager.LayoutParams) lp).getOrigHeight();
}
}
super.addView(child, index);
}
/**
* 获取某个childView在竖直方向所占的空间,将margin考虑进去
*
* @param view
* @return
*/
public int getDecoratedMeasurementVertical(View view) {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
return getDecoratedMeasuredHeight(view) + params.topMargin
+ params.bottomMargin;
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) {
View view = recycler.getViewForPosition(position);
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(widthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
}
public static void setMarginTopAndBottom(View view, int marginTop, int marginBottom) {
WeakReference<View> viewWeakReference = new WeakReference<>(view);
if (viewWeakReference.get() != null) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
if (params != null) {
params.topMargin = marginTop;
params.bottomMargin = marginBottom;
viewWeakReference.get().setLayoutParams(params);
}
}
}
public void setFrameLayoutChildInsets(View child, Rect newInsets, Rect oldInsets) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (child instanceof Insettable) {
((Insettable) child).setInsets(newInsets);
} else if (!lp.ignoreInsets) {
lp.topMargin += (newInsets.top - oldInsets.top);
lp.leftMargin += (newInsets.left - oldInsets.left);
lp.rightMargin += (newInsets.right - oldInsets.right);
lp.bottomMargin += (newInsets.bottom - oldInsets.bottom);
}
child.setLayoutParams(lp);
}
@Override
protected boolean onCustomMeasure(View view, int width, int height) {
if (view == videoView.getControlsView()) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = videoView.getMeasuredWidth();
layoutParams.height = videoView.getAspectRatioView().getMeasuredHeight() + (videoView.isInFullscreen() ? 0 : AndroidUtilities.dp(10));
}
return false;
}
private int getAbsoluteGravity(View menu) {
final int gravity = ((LayoutParams) menu.getLayoutParams()).gravity;
return GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this));
}
private void setWeight(int id, float progress) {
View before = findViewById(id);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) before.getLayoutParams();
layoutParams.weight = progress;
before.requestLayout();
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mVerticalDividerDrawableHeight <= 0 && mHorizontalDividerDrawableHeight <= 0) return;
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
int position = params.getViewAdapterPosition();
int headersCount;
int footerCount;
int itemViewCount;
FamiliarRecyclerView curFamiliarRecyclerView = null;
if (parent instanceof FamiliarRecyclerView) {
curFamiliarRecyclerView = (FamiliarRecyclerView) parent;
footerCount = curFamiliarRecyclerView.getFooterViewsCount();
headersCount = curFamiliarRecyclerView.getHeaderViewsCount();
itemViewCount = curFamiliarRecyclerView.getAdapter().getItemCount() - headersCount - footerCount;
} else {
headersCount = 0;
footerCount = 0;
itemViewCount = parent.getAdapter().getItemCount();
}
// intercept filter
if (isInterceptFilter(position, headersCount, footerCount, itemViewCount)) return;
// set headView or footerView
if (isHeadViewPos(headersCount, position)) {
// head
if (mOrientation == OrientationHelper.HORIZONTAL) {
outRect.set(mVerticalDividerDrawableHeight, 0, 0, 0);
} else {
outRect.set(0, mHorizontalDividerDrawableHeight, 0, 0);
}
return;
} else if (isFooterViewPos(headersCount, footerCount, itemViewCount, position)) {
// footer
if (mOrientation == OrientationHelper.HORIZONTAL) {
outRect.set(mVerticalDividerDrawableHeight, 0, 0, 0);
} else {
outRect.set(0, mHorizontalDividerDrawableHeight, 0, 0);
}
return;
} else if (isEmptyView(curFamiliarRecyclerView, position, headersCount)) {
// emptyView
if (isHeaderDividersEnabled && headersCount > 0) {
if (mOrientation == OrientationHelper.HORIZONTAL) {
outRect.set(mVerticalDividerDrawableHeight, 0, 0, 0);
} else {
outRect.set(0, mHorizontalDividerDrawableHeight, 0, 0);
}
}
return;
}
// set itemView
if (mLayoutManagerType == FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_GRID || mLayoutManagerType == FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_STAGGERED_GRID) {
processGridOffsets(outRect, position, headersCount, view);
} else {
int topOrLeftSize;
if ((!isHeaderDividersEnabled || headersCount == 0) && position - headersCount == 0) {
topOrLeftSize = 0;
} else {
topOrLeftSize = mOrientation == OrientationHelper.VERTICAL ? mHorizontalDividerDrawableHeight : mVerticalDividerDrawableHeight;
}
if (mOrientation == OrientationHelper.HORIZONTAL) {
outRect.set(topOrLeftSize, mItemViewBothSidesMargin, 0, mItemViewBothSidesMargin);
} else {
outRect.set(mItemViewBothSidesMargin, topOrLeftSize, mItemViewBothSidesMargin, 0);
}
}
}
/**
* 测试tabflowlayout竖直状态
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
private void measureTabVertical(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int childCount = getChildCount();
int width = 0;
int height = 0;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
measureChild(child, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
//拿到 子控件宽高 + margin
int cw = child.getMeasuredWidth() + params.leftMargin + params.rightMargin;
int ch = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
height += ch;
width = Math.max(width, cw);
}
if (MeasureSpec.EXACTLY == widthMode ) {
width = widthSize;
} else {
width += getPaddingLeft() + getPaddingRight();
}
if (MeasureSpec.EXACTLY == heightMode) {
height = heightSize;
} else {
height += getPaddingTop() + getPaddingBottom();
}
mViewHeight = height;
setMeasuredDimension(width, height);
}