@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSie = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(widthSie, heightSize); View bottomView = getChildAt(1); MarginLayoutParams lp = (MarginLayoutParams) bottomView.getLayoutParams(); int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec, lp.leftMargin + lp.rightMargin, lp.width); int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.bottomMargin + lp.topMargin, lp.height); bottomView.measure(drawerWidthSpec, drawerHeightSpec); View contentView = getChildAt(0); lp = (MarginLayoutParams) contentView.getLayoutParams(); int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSie - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY); int contentHeightSpec = MeasureSpec.makeMeasureSpec( heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY); contentView.measure(contentWidthSpec, contentHeightSpec); mBottomMenuView = bottomView; mContentView = contentView; }
private void setListViewHeight(ExpandableListView listView, int group) { ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter(); int totalHeight = 0; int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.EXACTLY); for (int i = 0; i < listAdapter.getGroupCount(); i++) { View groupItem = listAdapter.getGroupView(i, false, null, listView); groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += groupItem.getMeasuredHeight(); if (((listView.isGroupExpanded(i)) && (i != group)) || ((!listView.isGroupExpanded(i)) && (i == group))) { for (int j = 0; j < listAdapter.getChildrenCount(i); j++) { View listItem = listAdapter.getChildView(i, j, false, null, listView); listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } } } ViewGroup.LayoutParams params = listView.getLayoutParams(); int height = totalHeight + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1)); if (height < 10) height = 200; params.height = height; listView.setLayoutParams(params); listView.requestLayout(); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { throw new RuntimeException("SlidingDrawer cannot have UNSPECIFIED dimensions"); } final View handle = mHandle; measureChild(handle, widthMeasureSpec, heightMeasureSpec); if (mVertical) { int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset; mContent.measure( MeasureSpec.makeMeasureSpec(widthSpecSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); } else { int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset; mContent.measure( MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(heightSpecSize, MeasureSpec.EXACTLY)); } setMeasuredDimension(widthSpecSize, heightSpecSize); }
private void restrictDamagesListHeight() { if (adapter.getCount() < 5) { View item = adapter.getView(0, null, damagesListView); item.measure(0, 0); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( getResources().getDimensionPixelSize(R.dimen.component_standard_size), (int) (5.5 * item.getMeasuredHeight())); damagesListView.setLayoutParams(params); } if (adapter.getCount() >= 5) { // show no more than 8 damages, and not less than 5 int showNb = Math.min(adapter.getCount(), 5); View item = adapter.getView(0, null, damagesListView); item.measure(0, 0); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( getResources().getDimensionPixelSize(R.dimen.component_standard_size), (int) ((showNb + 0.5) * item.getMeasuredHeight())); damagesListView.setLayoutParams(params); } damagesListView.requestLayout(); damagesListView.getParent().requestLayout(); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { throw new RuntimeException("View cannot have UNSPECIFIED dimensions"); } mMainView.measure(widthMeasureSpec, heightMeasureSpec); int leftViewWidth = (int) (widthSpecSize - mLeftViewRightPadding); mLeftView.measure( MeasureSpec.makeMeasureSpec(leftViewWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(heightSpecSize, MeasureSpec.EXACTLY)); int rightViewWidth = (int) (widthSpecSize - mRightViewLeftPadding); mRightView.measure( MeasureSpec.makeMeasureSpec(rightViewWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(heightSpecSize, MeasureSpec.EXACTLY)); setMeasuredDimension(widthSpecSize, heightSpecSize); }
static int measureChildForCells( View child, int cellSize, int cellsRemaining, int parentHeightMeasureSpec, int parentHeightPadding) { LayoutParams lp = (LayoutParams) child.getLayoutParams(); int childHeightSpec = MeasureSpec.makeMeasureSpec( MeasureSpec.getSize(parentHeightMeasureSpec) - parentHeightPadding, MeasureSpec.getMode(parentHeightMeasureSpec)); ActionMenuItemView itemView = child instanceof ActionMenuItemView ? (ActionMenuItemView) child : null; boolean hasText = itemView != null && itemView.hasText(); int cellsUsed = 0; if (cellsRemaining > 0 && (!hasText || cellsRemaining >= 2)) { child.measure( MeasureSpec.makeMeasureSpec(cellSize * cellsRemaining, ExploreByTouchHelper.INVALID_ID), childHeightSpec); int measuredWidth = child.getMeasuredWidth(); cellsUsed = measuredWidth / cellSize; if (measuredWidth % cellSize != 0) { cellsUsed++; } if (hasText && cellsUsed < 2) { cellsUsed = 2; } } boolean expandable = !lp.isOverflowButton && hasText; lp.expandable = expandable; lp.cellsUsed = cellsUsed; child.measure(MeasureSpec.makeMeasureSpec(cellsUsed * cellSize, 1073741824), childHeightSpec); return cellsUsed; }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int widthWithoutPadding = width - mPaddingLeft - mPaddingRight; int heightWithoutPadding = height - mPaddingTop - mPaddingBottom; // Measure the content mContent.measure( MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.EXACTLY)); // Measure the bar view, and action button mHeaderView.measure( MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mConfig.taskBarHeight, MeasureSpec.EXACTLY)); mActionButtonView.measure( MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(heightWithoutPadding, MeasureSpec.AT_MOST)); // Measure the thumbnail to be square mThumbnailView.measure( MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.EXACTLY)); setMeasuredDimension(width, height); invalidateOutline(); }
private void measureHeader() { if (mStickiedHeader == null) { return; } int widthMeasureSpec; if (mHeadersIgnorePadding) { widthMeasureSpec = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY); } else { widthMeasureSpec = MeasureSpec.makeMeasureSpec( getWidth() - getPaddingLeft() - getPaddingRight(), MeasureSpec.EXACTLY); } int heightMeasureSpec = 0; ViewGroup.LayoutParams params = mStickiedHeader.getLayoutParams(); if (params != null && params.height > 0) { heightMeasureSpec = MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY); } else { heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } mStickiedHeader.measure(MeasureSpec.makeMeasureSpec(0, 0), MeasureSpec.makeMeasureSpec(0, 0)); mStickiedHeader.measure(widthMeasureSpec, heightMeasureSpec); if (mHeadersIgnorePadding) { mStickiedHeader.layout(getLeft(), 0, getRight(), mStickiedHeader.getMeasuredHeight()); } else { mStickiedHeader.layout( getLeft() + getPaddingLeft(), 0, getRight() - getPaddingRight(), mStickiedHeader.getMeasuredHeight()); } }
/** * Measure a child view to fit within cell-based formatting. The child's width will be measured to * a whole multiple of cellSize. * * <p> * * <p>Sets the expandable and cellsUsed fields of LayoutParams. * * @param child Child to measure * @param cellSize Size of one cell * @param cellsRemaining Number of cells remaining that this view can expand to fill * @param parentHeightMeasureSpec MeasureSpec used by the parent view * @param parentHeightPadding Padding present in the parent view * @return Number of cells this child was measured to occupy */ static int measureChildForCells( View child, int cellSize, int cellsRemaining, int parentHeightMeasureSpec, int parentHeightPadding) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final int childHeightSize = MeasureSpec.getSize(parentHeightMeasureSpec) - parentHeightPadding; final int childHeightMode = MeasureSpec.getMode(parentHeightMeasureSpec); final int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeightSize, childHeightMode); int cellsUsed = 0; if (cellsRemaining > 0) { final int childWidthSpec = MeasureSpec.makeMeasureSpec(cellSize * cellsRemaining, MeasureSpec.AT_MOST); child.measure(childWidthSpec, childHeightSpec); final int measuredWidth = child.getMeasuredWidth(); cellsUsed = measuredWidth / cellSize; if (measuredWidth % cellSize != 0) cellsUsed++; } final ActionMenuItemView itemView = child instanceof ActionMenuItemView ? (ActionMenuItemView) child : null; final boolean expandable = !lp.isOverflowButton && itemView != null && itemView.hasText(); lp.expandable = expandable; lp.cellsUsed = cellsUsed; final int targetWidth = cellsUsed * cellSize; child.measure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY), childHeightSpec); return cellsUsed; }
public void measureViews(int widthMeasureSpec, int heightMeasureSpec) { View v1 = findViewById(R.id.sliding_tab); v1.measure(v1.getLayoutParams().width + v1.getLeft() + v1.getRight(), heightMeasureSpec); View v2 = findViewById(R.id.activity_body); v2.measure(widthMeasureSpec, heightMeasureSpec); }
/** * Taken (almost) verbatum from system FrameLayout Completely ignores margins and child states. * Should probably only take Card views into account. */ @Override protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { final int count = getChildCount(); final boolean measureMatchParentChildren = MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY || MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY; mMatchParentChildren.clear(); int maxHeight = 0; int maxWidth = 0; for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { final LayoutParams lp = child.getLayoutParams(); final int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 0, lp.width); final int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, 0, lp.height); child.measure(childWidthMeasureSpec, childHeightMeasureSpec); maxWidth = Math.max(maxWidth, child.getMeasuredWidth()); maxHeight = Math.max(maxHeight, child.getMeasuredHeight()); if (measureMatchParentChildren) { if (lp.width == LayoutParams.MATCH_PARENT || lp.height == LayoutParams.MATCH_PARENT) { mMatchParentChildren.add(child); } } } } maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); setMeasuredDimension( resolveSize(maxWidth, widthMeasureSpec), resolveSize(maxHeight, heightMeasureSpec)); for (final View child : mMatchParentChildren) { final LayoutParams lp = child.getLayoutParams(); int childWidthMeasureSpec; int childHeightMeasureSpec; if (lp.width == LayoutParams.MATCH_PARENT) { childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY); } else { childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 0, lp.width); } if (lp.height == LayoutParams.MATCH_PARENT) { childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY); } else { childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, 0, lp.height); } child.measure(childWidthMeasureSpec, childHeightMeasureSpec); } }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int width = MeasureSpec.getSize(widthMeasureSpec); mBrightnessView.measure(exactly(width), MeasureSpec.UNSPECIFIED); final int brightnessHeight = mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop; mFooter.getView().measure(exactly(width), MeasureSpec.UNSPECIFIED); int r = -1; int c = -1; int rows = 0; for (TileRecord record : mRecords) { if (record.tileView.getVisibility() == GONE) continue; // wrap to next column if we've reached the max # of columns if (mUseMainTiles && r == 0 && c == 1) { r = 1; c = 0; } else if (r == -1 || c == (mColumns - 1)) { r++; c = 0; } else { c++; } record.row = r; record.col = c; rows = r + 1; } for (TileRecord record : mRecords) { if (record.tileView.getVisibility() == GONE) continue; final int cw = (mUseMainTiles && record.row == 0) ? mLargeCellWidth : mCellWidth; final int ch = (mUseMainTiles && record.row == 0) ? mLargeCellHeight : mCellHeight; record.tileView.measure(exactly(cw), exactly(ch)); } int h = rows == 0 ? brightnessHeight : (getRowTop(rows) + mPanelPaddingBottom); if (mFooter.hasFooter()) { h += mFooter.getView().getMeasuredHeight(); } mDetail.measure(exactly(width), MeasureSpec.UNSPECIFIED); if (isShowingDetail()) { Point size = new Point(); getDisplay().getSize(size); final int panelBottom = (mContainerTop - mTranslationTop + h); final int detailMinHeight = size.y - mContainerTop - mPanelPaddingBottom; if (size.y > panelBottom) { int delta = size.y - panelBottom; // panel is smaller than screen size mDetail.measure(exactly(width), exactly(detailMinHeight - delta)); } else { // panel is hanging below the screen mDetail.measure(exactly(width), exactly(detailMinHeight)); } } mGridHeight = h; setMeasuredDimension(width, Math.max(h, mDetail.getMeasuredHeight())); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(0, widthMeasureSpec); int height = getDefaultSize(0, heightMeasureSpec); setMeasuredDimension(width, height); final int contentWidth = getChildMeasureSpec(widthMeasureSpec, 0, width - mWidthOffset); final int contentHeight = getChildMeasureSpec(heightMeasureSpec, 0, height); mContent.measure(contentWidth, contentHeight); if (mSecondaryContent != null) mSecondaryContent.measure(contentWidth, contentHeight); }
protected void measureView(View v) { // See what size the view wants to be v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); // Work out a scale that will fit it to this view float scale = Math.min( (float) getWidth() / (float) v.getMeasuredWidth(), (float) getHeight() / (float) v.getMeasuredHeight()); // Use the fitting values scaled by our current scale factor v.measure( MeasureSpec.EXACTLY | (int) (v.getMeasuredWidth() * scale * mScale), MeasureSpec.EXACTLY | (int) (v.getMeasuredHeight() * scale * mScale)); }
/** {@inheritDoc} */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int screenWidth = MeasureSpec.getSize(widthMeasureSpec); // Compute the width of a tab as a fraction of the screen width final int tabWidth = Math.round(mTabWidthScreenFraction * screenWidth); // Find the allowed scrolling length by subtracting the current visible // screen width // from the total length of the tabs. mAllowedHorizontalScrollLength = tabWidth * TAB_COUNT - screenWidth; // Scrolling by mAllowedHorizontalScrollLength causes listeners to // scroll by the entire screen amount; compute the scale-factor // necessary to make this so. if (mAllowedHorizontalScrollLength == 0) { // Guard against divide-by-zero. // This hard-coded value prevents a crash, but won't result in the // desired scrolling behavior. We rely on the framework calling // onMeasure() // again with a non-zero screen width. mScrollScaleFactor = 1.0f; } else { mScrollScaleFactor = screenWidth / mAllowedHorizontalScrollLength; } final int tabHeight = Math.round(screenWidth * mTabHeightScreenFraction) + mTabShadowHeight; // Set the child layout's to be TAB_COUNT * the computed tab // width so that the layout's children (which are the tabs) will evenly // split that width. if (getChildCount() > 0) { final View child = getChildAt(0); // Add 1 dip of separation between the tabs final int seperatorPixels = (int) (TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()) + 0.5f); if (mDualTabs) { final int size = TAB_COUNT * tabWidth + (TAB_COUNT - 1) * seperatorPixels; child.measure(measureExact(size), measureExact(tabHeight)); } else { child.measure(measureExact(screenWidth), measureExact(tabHeight)); } } mAllowedVerticalScrollLength = tabHeight - mTabDisplayLabelHeight - mTabShadowHeight; setMeasuredDimension( resolveSize(screenWidth, widthMeasureSpec), resolveSize(tabHeight, heightMeasureSpec)); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(widthSize, heightSize); final boolean applyInsets = lastInsets != null && Build.VERSION.SDK_INT >= 21; final int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } final LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (applyInsets) { if (child.getFitsSystemWindows()) { dispatchChildInsets(child, lastInsets, lp.gravity); } else { applyMarginInsets(lp, lastInsets, lp.gravity, Build.VERSION.SDK_INT >= 21); } } if (drawerLayout != child) { final int contentWidthSpec = MeasureSpec.makeMeasureSpec( widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY); final int contentHeightSpec = MeasureSpec.makeMeasureSpec( heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY); child.measure(contentWidthSpec, contentHeightSpec); } else { child.setPadding(0, 0, 0, 0); final int drawerWidthSpec = getChildMeasureSpec( widthMeasureSpec, minDrawerMargin + lp.leftMargin + lp.rightMargin, lp.width); final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin, lp.height); child.measure(drawerWidthSpec, drawerHeightSpec); } } }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); } final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); PagedViewCellLayout.LayoutParams lp = (PagedViewCellLayout.LayoutParams) child.getLayoutParams(); lp.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap, getPaddingLeft(), getPaddingTop()); int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); child.measure(childWidthMeasureSpec, childheightMeasureSpec); } setMeasuredDimension(widthSpecSize, heightSpecSize); }
public static void expand(final View v) { v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); final int targetHeight = v.getMeasuredHeight(); v.getLayoutParams().height = 0; v.setVisibility(View.VISIBLE); Animation a = new Animation() { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { v.getLayoutParams().height = interpolatedTime == 1 ? LayoutParams.WRAP_CONTENT : (int) (targetHeight * interpolatedTime); v.requestLayout(); } @Override public boolean willChangeBounds() { return true; } }; // 1dp/ms a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density)); v.startAnimation(a); }
public HeaderDecoration(final Context context, RecyclerView parent, @LayoutRes int resId) { // inflate and measure the layout mLayout = LayoutInflater.from(context).inflate(resId, parent, false); mLayout.measure( View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); }
public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) return; int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED); int totalHeight = 0; View view = null; for (int i = 0; i < listAdapter.getCount(); i++) { view = listAdapter.getView(i, view, listView); if (i == 0) view.setLayoutParams( new ViewGroup.LayoutParams(desiredWidth, AbsListView.LayoutParams.MATCH_PARENT)); view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += view.getMeasuredHeight() + 20; } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
public int getRowHeight() { if (mRowHeight > 0) { return mRowHeight; } ListAdapter adapter = getAdapter(); int numColumns = getNumColumnsCompatible(); // adapter has not been set or has no views in it; if (adapter == null || adapter.getCount() <= numColumns * (mHeaderViewInfos.size() + mFooterViewInfos.size())) { return -1; } int mColumnWidth = getColumnWidthCompatible(); View view = getAdapter().getView(numColumns * mHeaderViewInfos.size(), mViewForMeasureRowHeight, this); AbsListView.LayoutParams p = (AbsListView.LayoutParams) view.getLayoutParams(); if (p == null) { p = new AbsListView.LayoutParams(-1, -2, 0); view.setLayoutParams(p); } int childHeightSpec = getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, p.height); int childWidthSpec = getChildMeasureSpec( MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY), 0, p.width); view.measure(childWidthSpec, childHeightSpec); mViewForMeasureRowHeight = view; mRowHeight = view.getMeasuredHeight(); return mRowHeight; }
public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = 0; listView.setLayoutParams(params); listView.requestLayout(); return; } int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom(); int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST); for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); if (listItem != null) { listItem.setLayoutParams( new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); listView.requestLayout(); }
private void prepView() { int i = android.view.c((int)TokenCompleteTextView.access$0(TokenCompleteTextView.this), 0x80000000); int j = android.view.c(0, 0); view.measure(i, j); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); }
public static void bound(Context context, View view, boolean isFullScreen) { // Don't need this?! if (!disableAdjusting) { int windowWidth = getWindowWidth(context); int windowHeight = getWindowHeight(context); int adjustHeight = 0; if (!isFullScreen && respectAdjustHeight) adjustHeight += getStatusBarHeight(context) * adjustHeightMultiplier; int widthSpec = View.MeasureSpec.makeMeasureSpec(windowWidth, View.MeasureSpec.EXACTLY); int heightSpec = View.MeasureSpec.makeMeasureSpec(windowHeight - adjustHeight, View.MeasureSpec.EXACTLY); int heightC = windowHeight - adjustHeight; Logging.logt( String.format( "Window height: %s; Window width: %s; " + "Adjust height: %s; status bar height: %s;" + "widthSpec: %s; heightSpec: %s", windowHeight, windowWidth, adjustHeight, getStatusBarHeight(context), widthSpec, heightSpec)); view.measure(widthSpec, heightSpec); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); } }
public static Bitmap getBitmapFromView(View v, int width, int height) { v.measure(width, height); v.layout(0, 0, width, height); Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888); v.draw(new Canvas(bmp)); return bmp; }
private void measureScrapChild( RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) { try { View view = recycler.getViewForPosition(0); // fix 动态添加时报IndexOutOfBoundsException if (view != null) { RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams(); int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, getPaddingLeft() + getPaddingRight(), p.width); int childHeightSpec = ViewGroup.getChildMeasureSpec( heightSpec, getPaddingTop() + getPaddingBottom(), p.height); view.measure(childWidthSpec, childHeightSpec); measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin; recycler.recycleView(view); } } catch (Exception e) { e.printStackTrace(); } finally { } }
public View createView() { inflater = LayoutInflater.from(context); content = inflater.inflate(contentResId, null); realHeader = inflater.inflate(headerResId, null); realHeaderLayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); realHeaderLayoutParams.gravity = Gravity.TOP; // Use measured height here as an estimate of the header height, later // on after the layout is complete // we'll use the actual height int widthMeasureSpec = MeasureSpec.makeMeasureSpec(LayoutParams.MATCH_PARENT, MeasureSpec.EXACTLY); int heightMeasureSpec = MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY); realHeader.measure(widthMeasureSpec, heightMeasureSpec); headerHeight = realHeader.getMeasuredHeight(); listView = (ListView) content.findViewById(android.R.id.list); if (listView != null) { createListView(); } else { createScrollView(); } return root; }
public void setAdapter(TaskDescriptionAdapter adapter) { Log.d(TAG, "adapter.getCount() = " + adapter.getCount()); mAdapter = adapter; mAdapter.registerDataSetObserver( new DataSetObserver() { public void onChanged() { update(); } public void onInvalidated() { update(); } }); DisplayMetrics dm = getResources().getDisplayMetrics(); int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.AT_MOST); int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.AT_MOST); View child = mAdapter.createView(mLinearLayout); child.measure(childWidthMeasureSpec, childheightMeasureSpec); mNumItemsInOneScreenful = (int) FloatMath.ceil(dm.widthPixels / (float) child.getMeasuredWidth()); addToRecycledViews(child); mCellWidth = child.getMeasuredWidth(); for (int i = 0; i < mNumItemsInOneScreenful - 1; i++) { addToRecycledViews(mAdapter.createView(mLinearLayout)); } }
public void measureChild(View child) { final LauncherAppState app = LauncherAppState.getInstance(); final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); final int cellWidth = mCellWidth; final int cellHeight = mCellHeight; CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); if (!lp.isFullscreen) { lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, invertLayoutHorizontally(), mCountX); if (child instanceof LauncherAppWidgetHostView) { // Widgets have their own padding, so skip } else { // Otherwise, center the icon int cHeight = getCellContentHeight(); int cellPaddingY = (int) Math.max(0, ((lp.height - cHeight) / 2f)); int cellPaddingX = (int) (grid.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); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (!mFillViewport) { return; } final int heightMode = MeasureSpec.getMode(heightMeasureSpec); if (heightMode == MeasureSpec.UNSPECIFIED) { return; } if (getChildCount() > 0) { final View child = getChildAt(0); int height = getMeasuredHeight(); if (child.getMeasuredHeight() < height) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, getPaddingLeft() + getPaddingRight(), lp.width); height -= getPaddingTop(); height -= getPaddingBottom(); int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); child.measure(childWidthMeasureSpec, childHeightMeasureSpec); } } }