下面列出了android.view.ViewGroup#LayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void take(View view, ViewGroup.LayoutParams params) {
ensureFirstPage();
DocumentPage page = getLastPage();
LOG.i("take:", "view:", Utils.mark(view), "dispatching to page", page.getNumber());
boolean empty = page.getChildCount() == 0;
if (page.canTake(view, params, false)) {
page.take(view, params);
} else if (!empty) {
// Could not take but had views. Open another.
LOG.i("take:", "view:", Utils.mark(view), "could not take. Opening another.");
openPage();
take(view, params);
} else {
// This view is untakable, doesn't fit the page size.
// Pass anyway, it will be cropped but who cares.
LOG.w("take:", "view:", Utils.mark(view), "is untakable, appending to page", getLastPage().getNumber());
Utils.setUntakable(view, true);
getLastPage().take(view, params);
LOG.e("take:",
"Got a child but it is too tall to fit a single page.",
"Please split into multiple childs");
}
}
/**
* 初始化必要的View,并指明视图显示宽高的参数
*/
private void init() {
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (view_loading == null) {
view_loading = UIUtils.getView(R.layout.page_loading);
addView(view_loading, params);
}
if (view_error == null) {
view_error = UIUtils.getView(R.layout.page_error);
addView(view_error, params);
}
if (view_empty == null) {
view_empty = UIUtils.getView(R.layout.page_empty);
addView(view_empty, params);
}
showSafePage();
}
@Override
public View getItemView(MenuItemImpl item, View convertView, ViewGroup parent) {
View actionView = item.getActionView();
if (actionView == null || item.hasCollapsibleActionView()) {
if (!(convertView instanceof ActionMenuItemView)) {
convertView = null;
}
actionView = super.getItemView(item, convertView, parent);
}
actionView.setVisibility(item.isActionViewExpanded() ? View.GONE : View.VISIBLE);
final ActionMenuView menuParent = (ActionMenuView) parent;
final ViewGroup.LayoutParams lp = actionView.getLayoutParams();
if (!menuParent.checkLayoutParams(lp)) {
actionView.setLayoutParams(menuParent.generateLayoutParams(lp));
}
return actionView;
}
public View inflateViews() {
mScrollView = new ScrollView(getActivity());
ViewGroup.LayoutParams scrollParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mScrollView.setLayoutParams(scrollParams);
mLogView = new LogView(getActivity());
ViewGroup.LayoutParams logParams = new ViewGroup.LayoutParams(scrollParams);
logParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
mLogView.setLayoutParams(logParams);
mLogView.setClickable(true);
mLogView.setFocusable(true);
mLogView.setTypeface(Typeface.MONOSPACE);
// Want to set padding as 16 dips, setPadding takes pixels. Hooray math!
int paddingDips = 16;
double scale = getResources().getDisplayMetrics().density;
int paddingPixels = (int) ((paddingDips * (scale)) + .5);
mLogView.setPadding(paddingPixels, paddingPixels, paddingPixels, paddingPixels);
mLogView.setCompoundDrawablePadding(paddingPixels);
mLogView.setGravity(Gravity.BOTTOM);
mLogView.setTextAppearance(getActivity(), android.R.style.TextAppearance_Holo_Medium);
mScrollView.addView(mLogView);
return mScrollView;
}
@Override
public View createView(Context context, int position, Object data) {
// 返回mImageView页面布局
ImageView imageView = new ImageView(context);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
);
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
Glide.with(context).load(data).into(imageView);
return imageView;
}
@Override
@SuppressWarnings("unchecked")
@CallSuper
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (params == null) {
params = generateDefaultLayoutParams();
} else {
params = generateLayoutParams(params);
}
if (child instanceof IRefreshView) {
IRefreshView<IIndicator> view = (IRefreshView<IIndicator>) child;
switch (view.getType()) {
case IRefreshView.TYPE_HEADER:
if (mHeaderView != null)
throw new IllegalArgumentException(
"Unsupported operation, HeaderView only can be add once !!");
mHeaderView = view;
break;
case IRefreshView.TYPE_FOOTER:
if (mFooterView != null)
throw new IllegalArgumentException(
"Unsupported operation, FooterView only can be add once !!");
mFooterView = view;
break;
}
}
super.addView(child, index, params);
}
/**
* added by fee 2019-07-11:
* <P>注:该方法主要是用来relayout适配的itemView布局,依赖的前提条件为外部指定了itemView的宽、高</P>
* 为了满足某些场景下,需要通过外部来重新指定item view的宽、高,则在{convert(xx,xx)}
* 需要重新设置item view的布局参数
* @param assignTagId 所指定的给 theItemView setTag()用于标记已经relayout过的
* @param theItemView 当前的item 视图
* @return true:重置了itemview布局
*/
protected boolean relayoutTheItemView(@IdRes int assignTagId, View theItemView) {
if (theAssignItemHeight == 0 && theAssignItemWidth == 0) {
return false;
}
if (theItemView == null) {
return false;
}
Object relayoutTag = theItemView.getTag(assignTagId);
if (relayoutTag != null) {//已经relayout了当前的item view,则不进行relayout了
return false;
}
startRelayoutItemView(theItemView);//这里回调出去主要是为了如果有额外的处理,则给机会处理,然后最后统一setLayoutParams
ViewGroup.LayoutParams vlp = theItemView.getLayoutParams();
if (vlp == null) {
vlp = new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
}
if (theAssignItemWidth > 0) {//一般宽较容易确定
vlp.width = theAssignItemWidth;
}
if (theAssignUiDesignWidthRatio != 0 && theAssignUiDesignHeightRatio != 0 && theAssignItemWidth > 0) {
//h实 = w实 * h设计高占比 /w设计宽占比:即等比缩放itemView宽高
theAssignItemHeight = theAssignItemWidth * theAssignUiDesignHeightRatio / theAssignUiDesignWidthRatio;
}
if (theAssignItemHeight > 0) {
vlp.height = theAssignItemHeight;
}
theItemView.setTag(assignTagId, "relayouted");
theItemView.setLayoutParams(vlp);
return true;
}
public void addFooterView(View v, Object data, boolean isSelectable) {
ListAdapter mAdapter = getAdapter();
if (mAdapter != null && !(mAdapter instanceof HeaderViewGridAdapter)) {
throw new IllegalStateException(
"Cannot add header view to grid -- setAdapter has already been called.");
}
ViewGroup.LayoutParams lyp = v.getLayoutParams();
FixedViewInfo info = new FixedViewInfo();
FrameLayout fl = new FullWidthFixedViewLayout(getContext());
if (lyp != null) {
v.setLayoutParams(new FrameLayout.LayoutParams(lyp.width, lyp.height));
fl.setLayoutParams(new AbsListView.LayoutParams(lyp.width, lyp.height));
}
fl.addView(v);
info.view = v;
info.viewContainer = fl;
info.data = data;
info.isSelectable = isSelectable;
mFooterViewInfos.add(info);
if (mAdapter != null) {
((HeaderViewGridAdapter) mAdapter).notifyDataSetChanged();
}
}
/**
* 调整SurfaceView的大小比例 , 以避免预览变形
*
* @param previewSize 预览大小
*/
private void adjustSurfaceViewSize(Size previewSize) {
int width = getResources().getDisplayMetrics().widthPixels;
int height = (int) (width * ((previewSize.getWidth() * 1.0f) / previewSize.getHeight()));
ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mSurfaceView.getLayoutParams();
lp.width = width;
lp.height = height;
mSurfaceView.setLayoutParams(lp);
}
public static void placeViewAt(final View view, final int x, final int y, final int w,
final int h) {
final ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp instanceof MarginLayoutParams) {
final MarginLayoutParams marginLayoutParams = (MarginLayoutParams)lp;
marginLayoutParams.width = w;
marginLayoutParams.height = h;
marginLayoutParams.setMargins(x, y, -50, 0);
}
}
public View inflateViews() {
mScrollView = new ScrollView(getActivity());
ViewGroup.LayoutParams scrollParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mScrollView.setLayoutParams(scrollParams);
mLogView = new LogView(getActivity());
ViewGroup.LayoutParams logParams = new ViewGroup.LayoutParams(scrollParams);
logParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
mLogView.setLayoutParams(logParams);
mLogView.setClickable(true);
mLogView.setFocusable(true);
mLogView.setTypeface(Typeface.MONOSPACE);
// Want to set padding as 16 dips, setPadding takes pixels. Hooray math!
int paddingDips = 16;
double scale = getResources().getDisplayMetrics().density;
int paddingPixels = (int) ((paddingDips * (scale)) + .5);
mLogView.setPadding(paddingPixels, paddingPixels, paddingPixels, paddingPixels);
mLogView.setCompoundDrawablePadding(paddingPixels);
mLogView.setGravity(Gravity.BOTTOM);
mLogView.setTextAppearance(getActivity(), android.R.style.TextAppearance_Holo_Medium);
mScrollView.addView(mLogView);
return mScrollView;
}
public static boolean addViewToWindow(Context context, View view, ViewGroup.LayoutParams params) {
if (null == view || null == context || !removeViewFromParent(view)) {
return false;
}
final WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
addView(view, params, manager);
return true;
}
/**
* @return the span index of {@link StaggeredGridLayoutManager.LayoutParams} and {@link GridLayoutManager.LayoutParams},
* otherwise 1 for all other {@link ViewGroup.LayoutParams}
*/
public static <T extends ViewGroup.LayoutParams> int getSpanIndex(@Nullable final T layoutParams) {
if (layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
return ((StaggeredGridLayoutManager.LayoutParams) layoutParams).getSpanIndex();
} else if (layoutParams instanceof GridLayoutManager.LayoutParams) {
return ((GridLayoutManager.LayoutParams) layoutParams).getSpanIndex();
} else {
return -1;
}
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override public void addView(View child, int index, ViewGroup.LayoutParams params) {
child.setOnClickListener(this);
super.addView(child, index, params);
}
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams()
{
return new LayoutParams(super.generateDefaultLayoutParams());
}
/**
* Used internally for adding view. Need because we override addView to
* pass-through to the Refreshable View
*/
protected final void addViewInternal(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
}
/**
* Hide the SuperCardToast and animate the Layout. Post Honeycomb only. *
*/
@SuppressLint("NewApi")
private void dismissWithLayoutAnimation() {
if (mToastView != null) {
mToastView.setVisibility(View.INVISIBLE);
final ViewGroup.LayoutParams layoutParams = mToastView.getLayoutParams();
final int originalHeight = mToastView.getHeight();
ValueAnimator animator = ValueAnimator.ofInt(originalHeight, 1)
.setDuration(mActivity.getResources().getInteger(android.R.integer.config_shortAnimTime));
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
Handler mHandler = new Handler();
mHandler.post(mHideImmediateRunnable);
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
@SuppressWarnings("ConstantConditions")
public void onAnimationUpdate(ValueAnimator valueAnimator) {
if (mToastView != null) {
try {
layoutParams.height = (Integer) valueAnimator.getAnimatedValue();
mToastView.setLayoutParams(layoutParams);
} catch (NullPointerException e) {
/* Do nothing */
}
}
}
});
animator.start();
} else {
dismissImmediately();
}
}