下面列出了android.view.ViewGroup.LayoutParams#WRAP_CONTENT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public BottomMenu(Activity context,OnClickListener clickListener) {
LayoutInflater inflater = LayoutInflater.from(context);
this.clickListener=clickListener;
mContext=context;
mMenuView = inflater.inflate(R.layout.layout_popwindow, null);
btn_wxLogin = (Button) mMenuView.findViewById(R.id.btn_wxLogin);
btn_msgCheckLogin = (Button) mMenuView.findViewById(R.id.btn_msgCheckLogin);
btnCancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
btnCancel.setOnClickListener(this);
btn_wxLogin.setOnClickListener(this);
btn_msgCheckLogin.setOnClickListener(this);
popupWindow=new PopupWindow(mMenuView,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,true);
popupWindow.setAnimationStyle(R.style.popwin_anim_style);
ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.white));
popupWindow.setBackgroundDrawable(dw);
mMenuView.setOnTouchListener(this);
}
/**
* 初始化所有POPUPWINDOW
*/
private void setPop() {
popupwindwow = this.getLayoutInflater().inflate(R.layout.bookpop, null);
toolpop = this.getLayoutInflater().inflate(R.layout.toolpop, null);
mPopupWindow = new PopupWindow(popupwindwow, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
// mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);
mToolpop = new PopupWindow(toolpop, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
toolpop1 = this.getLayoutInflater().inflate(R.layout.tool_size, null);
mToolpop1 = new PopupWindow(toolpop1, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
toolpop2 = this.getLayoutInflater().inflate(R.layout.tool_light, null);
mToolpop2 = new PopupWindow(toolpop2, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
toolpop3 = this.getLayoutInflater().inflate(R.layout.tool_mark, null);
mToolpop3 = new PopupWindow(toolpop3, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
toolpop4 = this.getLayoutInflater().inflate(R.layout.tool_jump, null);
mToolpop4 = new PopupWindow(toolpop4, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
playView = this.getLayoutInflater().inflate(R.layout.play_pop, null);
playpop = new PopupWindow(playView, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
private RelativeLayout createTitleView(Context context) {
RelativeLayout view = new RelativeLayout(context);
view.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView title = new TextView(context);
title.setPadding(0, SizeUtil.dp5, 0, SizeUtil.dp5);
title.setGravity(Gravity.CENTER);
title.setText(options.getTitle());
title.setTextColor(options.getTitleColor());
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, SizeUtil.textSize0);
title.setTypeface(null, Typeface.BOLD);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
title.setLayoutParams(layoutParams);
view.addView(title, title.getLayoutParams());
return view;
}
/**
* Same as previous, but with given size in case subTreeRoot itself has layout_width or layout_height = "wrap_content"
*/
private static void wrapContentAgain(ViewGroup subTreeRoot, boolean relayoutAllNodes,
int subTreeRootWidthMeasureSpec, int subTreeRootHeightMeasureSpec)
{
assert( "main".equals( Thread.currentThread().getName() ) );
if(subTreeRoot == null)
return;
LayoutParams layoutParams = subTreeRoot.getLayoutParams();
// --- First, we force measure on the subTree
int widthMeasureSpec = subTreeRootWidthMeasureSpec;
// When LayoutParams.MATCH_PARENT and Width > 0, we apply measured width to avoid getting dimensions too big
if( layoutParams.width != LayoutParams.WRAP_CONTENT && subTreeRoot.getWidth() > 0 )
widthMeasureSpec = MeasureSpec.makeMeasureSpec( subTreeRoot.getWidth(), MeasureSpec.EXACTLY );
int heightMeasureSpec = subTreeRootHeightMeasureSpec;
// When LayoutParams.MATCH_PARENT and Height > 0, we apply measured height to avoid getting dimensions too big
if( layoutParams.height != LayoutParams.WRAP_CONTENT && subTreeRoot.getHeight() > 0 )
heightMeasureSpec = MeasureSpec.makeMeasureSpec( subTreeRoot.getHeight(), MeasureSpec.EXACTLY );
// This measure recursively the whole sub-tree
subTreeRoot.measure( widthMeasureSpec, heightMeasureSpec );
// --- Then recurse on all children to correct the sizes
recurseWrapContent( subTreeRoot, relayoutAllNodes );
// --- RequestLayout to finish properly
subTreeRoot.requestLayout();
}
private void addSingleEditText(Context context, LinearLayout mainLayout, String singleValue, boolean readonly, int type) {
EditText editView = new EditText(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(15, 25, 15, 15);
editView.setLayoutParams(params);
editView.setText(singleValue);
editView.setEnabled(!readonly);
switch (type) {
case 1:
editView.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
break;
case 2:
editView.setInputType(InputType.TYPE_CLASS_PHONE);
break;
case 3:
editView.setInputType(InputType.TYPE_CLASS_DATETIME);
break;
case 4:
editView.setInputType(InputType.TYPE_CLASS_NUMBER);
break;
default:
break;
}
mainLayout.addView(editView);
editViewList.add(editView);
}
private void initBottom(LinearLayout llBottom, float ratio) {
LinearLayout llAt = new LinearLayout(activity);
llAt.setPadding(0, 0, 0, 5);
llAt.setBackgroundColor(0xffffffff);
int bottomHeight = (int) (DESIGN_BOTTOM_HEIGHT * ratio);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, bottomHeight);
llBottom.addView(llAt, lp);
tvAt = new TextView(activity);
tvAt.setTextColor(0xff3b3b3b);
tvAt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 21);
tvAt.setGravity(Gravity.BOTTOM);
tvAt.setText("@");
int padding = (int) (DESIGN_LEFT_PADDING * ratio);
tvAt.setPadding(padding, 0, padding, 0);
lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
llAt.addView(tvAt, lp);
tvAt.setOnClickListener(this);
if (isShowAtUserLayout(platform.getName())) {
tvAt.setVisibility(View.VISIBLE);
} else {
tvAt.setVisibility(View.INVISIBLE);
}
tvTextCouter = new TextView(activity);
tvTextCouter.setTextColor(0xff3b3b3b);
tvTextCouter.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTextCouter.setGravity(Gravity.BOTTOM | Gravity.RIGHT);
onTextChanged(etContent.getText(), 0, 0, 0);
tvTextCouter.setPadding(padding, 0, padding, 0);
lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lp.weight = 1;
llAt.addView(tvTextCouter, lp);
View v = new View(activity);
v.setBackgroundColor(0xffcccccc);
int px_1 = ratio > 1 ? ((int) ratio) : 1;
lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, px_1);
llBottom.addView(v, lp);
}
private void applyLayoutParams() {
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
switch (badgePosition) {
case POSITION_TOP_LEFT:
lp.gravity = Gravity.LEFT | Gravity.TOP;
lp.setMargins(badgeMarginH, badgeMarginV, 0, 0);
break;
case POSITION_TOP_RIGHT:
lp.gravity = Gravity.RIGHT | Gravity.TOP;
lp.setMargins(0, badgeMarginV, badgeMarginH, 0);
break;
case POSITION_BOTTOM_LEFT:
lp.gravity = Gravity.LEFT | Gravity.BOTTOM;
lp.setMargins(badgeMarginH, 0, 0, badgeMarginV);
break;
case POSITION_BOTTOM_RIGHT:
lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
lp.setMargins(0, 0, badgeMarginH, badgeMarginV);
break;
case POSITION_CENTER:
lp.gravity = Gravity.CENTER;
lp.setMargins(0, 0, 0, 0);
break;
default:
break;
}
setLayoutParams(lp);
}
@Override
/**
* Ask the host application for a custom progress view to show while
* a <video> is loading.
* @return View The progress view.
*/
public View getVideoLoadingProgressView() {
if (mVideoProgressView == null) {
// Create a new Loading view programmatically.
// create the linear layout
LinearLayout layout = new LinearLayout(parentEngine.getView().getContext());
layout.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.setLayoutParams(layoutParams);
// the proress bar
ProgressBar bar = new ProgressBar(parentEngine.getView().getContext());
LinearLayout.LayoutParams barLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
barLayoutParams.gravity = Gravity.CENTER;
bar.setLayoutParams(barLayoutParams);
layout.addView(bar);
mVideoProgressView = layout;
}
return mVideoProgressView;
}
private void initBody(RelativeLayout rlBody, float ratio) {
svContent = new ScrollView(activity);
rlBody.addView(svContent, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout llContent = new LinearLayout(activity);
llContent.setOrientation(LinearLayout.VERTICAL);
svContent.addView(llContent, new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
etContent = new EditText(activity);
int padding = (int) (DESIGN_LEFT_PADDING * ratio);
etContent.setPadding(padding, padding, padding, padding);
etContent.setBackgroundDrawable(null);
etContent.setTextColor(0xff3b3b3b);
etContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, 21);
etContent.setText(sp.getText());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llContent.addView(etContent, lp);
etContent.addTextChangedListener(this);
rlThumb = new RelativeLayout(activity);
rlThumb.setBackgroundColor(0xff313131);
int thumbWidth = (int) (DESIGN_THUMB_HEIGHT * ratio);
int xWidth = (int) (DESIGN_REMOVE_THUMB_HEIGHT * ratio);
lp = new LinearLayout.LayoutParams(thumbWidth, thumbWidth);
lp.leftMargin = lp.rightMargin = lp.bottomMargin = lp.topMargin = padding;
llContent.addView(rlThumb, lp);
aivThumb = new AsyncImageView(activity) {
public void onImageGot(String url, Bitmap bm) {
thumb = bm;
super.onImageGot(url, bm);
}
};
aivThumb.setScaleToCropCenter(true);
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(thumbWidth, thumbWidth);
rlThumb.addView(aivThumb, rllp);
aivThumb.setOnClickListener(this);
initThumb(aivThumb);
xvRemove = new XView(activity);
xvRemove.setRatio(ratio);
rllp = new RelativeLayout.LayoutParams(xWidth, xWidth);
rllp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rllp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlThumb.addView(xvRemove, rllp);
xvRemove.setOnClickListener(this);
}
private void initBody(RelativeLayout rlBody, float ratio) {
svContent = new ScrollView(activity);
rlBody.addView(svContent, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout llContent = new LinearLayout(activity);
llContent.setOrientation(LinearLayout.VERTICAL);
svContent.addView(llContent, new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
etContent = new EditText(activity);
int padding = (int) (DESIGN_LEFT_PADDING * ratio);
etContent.setPadding(padding, padding, padding, padding);
etContent.setBackgroundDrawable(null);
etContent.setTextColor(0xff3b3b3b);
etContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, 21);
etContent.setText(sp.getText());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llContent.addView(etContent, lp);
etContent.addTextChangedListener(this);
rlThumb = new RelativeLayout(activity);
rlThumb.setBackgroundColor(0xff313131);
int thumbWidth = (int) (DESIGN_THUMB_HEIGHT * ratio);
int xWidth = (int) (DESIGN_REMOVE_THUMB_HEIGHT * ratio);
lp = new LinearLayout.LayoutParams(thumbWidth, thumbWidth);
lp.leftMargin = lp.rightMargin = lp.bottomMargin = lp.topMargin = padding;
llContent.addView(rlThumb, lp);
aivThumb = new AsyncImageView(activity) {
public void onImageGot(String url, Bitmap bm) {
thumb = bm;
super.onImageGot(url, bm);
}
};
aivThumb.setScaleToCropCenter(true);
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(thumbWidth, thumbWidth);
rlThumb.addView(aivThumb, rllp);
aivThumb.setOnClickListener(this);
initThumb(aivThumb);
xvRemove = new XView(activity);
xvRemove.setRatio(ratio);
rllp = new RelativeLayout.LayoutParams(xWidth, xWidth);
rllp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rllp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlThumb.addView(xvRemove, rllp);
xvRemove.setOnClickListener(this);
}
/**
* Loads the image for the view if it isn't already loaded.
* @param isInLayoutPass True if this was invoked from a layout pass, false otherwise.
*/
void loadImageIfNecessary(final boolean isInLayoutPass) {
int width = getWidth();
int height = getHeight();
ScaleType scaleType = getScaleType();
boolean wrapWidth = false, wrapHeight = false;
if (getLayoutParams() != null) {
wrapWidth = getLayoutParams().width == LayoutParams.WRAP_CONTENT;
wrapHeight = getLayoutParams().height == LayoutParams.WRAP_CONTENT;
}
// if the view's bounds aren't known yet, and this is not a wrap-content/wrap-content
// view, hold off on loading the image.
boolean isFullyWrapContent = wrapWidth && wrapHeight;
if (width == 0 && height == 0 && !isFullyWrapContent) {
return;
}
// if the URL to be loaded in this view is empty, cancel any old requests and clear the
// currently loaded image.
if (TextUtils.isEmpty(mUrl)) {
if (mImageContainer != null) {
mImageContainer.cancelRequest();
mImageContainer = null;
}
setDefaultImageOrNull();
return;
}
// if there was an old request in this view, check if it needs to be canceled.
if (mImageContainer != null && mImageContainer.getRequestUrl() != null) {
if (mImageContainer.getRequestUrl().equals(mUrl)) {
// if the request is from the same URL, return.
return;
} else {
// if there is a pre-existing request, cancel it if it's fetching a different URL.
mImageContainer.cancelRequest();
setDefaultImageOrNull();
}
}
// Calculate the max image width / height to use while ignoring WRAP_CONTENT dimens.
int maxWidth = wrapWidth ? 0 : width;
int maxHeight = wrapHeight ? 0 : height;
// The pre-existing content of this view didn't match the current URL. Load the new image
// from the network.
ImageContainer newContainer = mImageLoader.get(mUrl,
new ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (mErrorImageId != 0) {
setImageResource(mErrorImageId);
}
}
@Override
public void onResponse(final ImageContainer response, boolean isImmediate) {
// If this was an immediate response that was delivered inside of a layout
// pass do not set the image immediately as it will trigger a requestLayout
// inside of a layout. Instead, defer setting the image by posting back to
// the main thread.
if (isImmediate && isInLayoutPass) {
post(new Runnable() {
@Override
public void run() {
onResponse(response, false);
}
});
return;
}
if (response.getBitmap() != null) {
setImageBitmap(response.getBitmap());
} else if (mDefaultImageId != 0) {
setImageResource(mDefaultImageId);
}
}
}, maxWidth, maxHeight, scaleType);
// update the ImageContainer to be the new bitmap container.
mImageContainer = newContainer;
}
private void initTitle(RelativeLayout rlTitle, float ratio) {
tvCancel = new TextView(activity);
tvCancel.setTextColor(0xff3b3b3b);
tvCancel.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvCancel.setGravity(Gravity.CENTER);
int resId = ResHelper.getStringRes(activity, "ssdk_oks_cancel");
if (resId > 0) {
tvCancel.setText(resId);
}
int padding = (int) (DESIGN_LEFT_PADDING * ratio);
tvCancel.setPadding(padding, 0, padding, 0);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rlTitle.addView(tvCancel, lp);
tvCancel.setOnClickListener(this);
TextView tvTitle = new TextView(activity);
tvTitle.setTextColor(0xff3b3b3b);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
tvTitle.setGravity(Gravity.CENTER);
resId = ResHelper.getStringRes(activity, "ssdk_oks_multi_share");
if (resId > 0) {
tvTitle.setText(resId);
}
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
rlTitle.addView(tvTitle, lp);
tvShare = new TextView(activity);
tvShare.setTextColor(0xffff6d11);
tvShare.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvShare.setGravity(Gravity.CENTER);
resId = ResHelper.getStringRes(activity, "ssdk_oks_share");
if (resId > 0) {
tvShare.setText(resId);
}
tvShare.setPadding(padding, 0, padding, 0);
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlTitle.addView(tvShare, lp);
tvShare.setOnClickListener(this);
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
ViewGroup view = (ViewGroup) convertView;
ViewHolder viewHolder;
if (view == null) {
view = createView(parent);
viewHolder = new ViewHolder();
viewHolder.titleParent = (ViewGroup) view.findViewById(mTitleParentResId);
viewHolder.contentParent = (ViewGroup) view.findViewById(mContentParentResId);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
View titleView = getTitleView(position, viewHolder.titleView, viewHolder.titleParent);
if (titleView != viewHolder.titleView) {
viewHolder.titleParent.removeAllViews();
viewHolder.titleParent.addView(titleView);
if (mActionViewResId == 0) {
view.setOnClickListener(new TitleViewOnClickListener(viewHolder.contentParent));
} else {
view.findViewById(mActionViewResId).setOnClickListener(new TitleViewOnClickListener(viewHolder.contentParent));
}
}
viewHolder.titleView = titleView;
View contentView = getContentView(position, viewHolder.contentView, viewHolder.contentParent);
if (contentView != viewHolder.contentView) {
viewHolder.contentParent.removeAllViews();
viewHolder.contentParent.addView(contentView);
}
viewHolder.contentView = contentView;
viewHolder.contentParent.setVisibility(mExpandedIds.contains(getItemId(position)) ? View.VISIBLE : View.GONE);
viewHolder.contentParent.setTag(getItemId(position));
LayoutParams layoutParams = viewHolder.contentParent.getLayoutParams();
layoutParams.height = LayoutParams.WRAP_CONTENT;
viewHolder.contentParent.setLayoutParams(layoutParams);
return view;
}
private void initBody(RelativeLayout rlBody, float ratio) {
svContent = new ScrollView(activity);
rlBody.addView(svContent, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout llContent = new LinearLayout(activity);
llContent.setOrientation(LinearLayout.VERTICAL);
svContent.addView(llContent, new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
etContent = new EditText(activity);
int padding = (int) (DESIGN_LEFT_PADDING * ratio);
etContent.setPadding(padding, padding, padding, padding);
etContent.setBackgroundDrawable(null);
etContent.setTextColor(0xff3b3b3b);
etContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, 21);
etContent.setText(sp.getText());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llContent.addView(etContent, lp);
etContent.addTextChangedListener(this);
rlThumb = new RelativeLayout(activity);
rlThumb.setBackgroundColor(0xff313131);
int thumbWidth = (int) (DESIGN_THUMB_HEIGHT * ratio);
int xWidth = (int) (DESIGN_REMOVE_THUMB_HEIGHT * ratio);
lp = new LinearLayout.LayoutParams(thumbWidth, thumbWidth);
lp.leftMargin = lp.rightMargin = lp.bottomMargin = lp.topMargin = padding;
llContent.addView(rlThumb, lp);
aivThumb = new AsyncImageView(activity) {
public void onImageGot(String url, Bitmap bm) {
thumb = bm;
super.onImageGot(url, bm);
}
};
aivThumb.setScaleToCropCenter(true);
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(thumbWidth, thumbWidth);
rlThumb.addView(aivThumb, rllp);
aivThumb.setOnClickListener(this);
initThumb(aivThumb);
xvRemove = new XView(activity);
xvRemove.setRatio(ratio);
rllp = new RelativeLayout.LayoutParams(xWidth, xWidth);
rllp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rllp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlThumb.addView(xvRemove, rllp);
xvRemove.setOnClickListener(this);
}
private void initTitle(RelativeLayout rlTitle, float ratio) {
tvCancel = new TextView(activity);
tvCancel.setTextColor(0xff3b3b3b);
tvCancel.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvCancel.setGravity(Gravity.CENTER);
int resId = R.getStringRes(activity, "ssdk_oks_cancel");
if (resId > 0) {
tvCancel.setText(resId);
}
int padding = (int) (DESIGN_LEFT_PADDING * ratio);
tvCancel.setPadding(padding, 0, padding, 0);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rlTitle.addView(tvCancel, lp);
tvCancel.setOnClickListener(this);
TextView tvTitle = new TextView(activity);
tvTitle.setTextColor(0xff3b3b3b);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
tvTitle.setGravity(Gravity.CENTER);
resId = R.getStringRes(activity, "ssdk_oks_multi_share");
if (resId > 0) {
tvTitle.setText(resId);
}
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
rlTitle.addView(tvTitle, lp);
tvShare = new TextView(activity);
tvShare.setTextColor(0xffff6d11);
tvShare.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvShare.setGravity(Gravity.CENTER);
resId = R.getStringRes(activity, "ssdk_oks_share");
if (resId > 0) {
tvShare.setText(resId);
}
tvShare.setPadding(padding, 0, padding, 0);
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlTitle.addView(tvShare, lp);
tvShare.setOnClickListener(this);
}
/**
* Loads the image for the view if it isn't already loaded.
* @param isInLayoutPass True if this was invoked from a layout pass, false otherwise.
*/
void loadImageIfNecessary(final boolean isInLayoutPass) {
int width = getWidth();
int height = getHeight();
ScaleType scaleType = getScaleType();
boolean wrapWidth = false, wrapHeight = false;
if (getLayoutParams() != null) {
wrapWidth = getLayoutParams().width == LayoutParams.WRAP_CONTENT;
wrapHeight = getLayoutParams().height == LayoutParams.WRAP_CONTENT;
}
// if the view's bounds aren't known yet, and this is not a wrap-content/wrap-content
// view, hold off on loading the image.
boolean isFullyWrapContent = wrapWidth && wrapHeight;
if (width == 0 && height == 0 && !isFullyWrapContent) {
return;
}
// if the URL to be loaded in this view is empty, cancel any old requests and clear the
// currently loaded image.
if (TextUtils.isEmpty(mUrl)) {
if (mImageContainer != null) {
mImageContainer.cancelRequest();
mImageContainer = null;
}
setDefaultImageOrNull();
return;
}
// if there was an old request in this view, check if it needs to be canceled.
if (mImageContainer != null && mImageContainer.getRequestUrl() != null) {
if (mImageContainer.getRequestUrl().equals(mUrl)) {
// if the request is from the same URL, return.
return;
} else {
// if there is a pre-existing request, cancel it if it's fetching a different URL.
mImageContainer.cancelRequest();
setDefaultImageOrNull();
}
}
// Calculate the max image width / height to use while ignoring WRAP_CONTENT dimens.
int maxWidth = wrapWidth ? 0 : width;
int maxHeight = wrapHeight ? 0 : height;
// The pre-existing content of this view didn't match the current URL. Load the new image
// from the network.
ImageContainer newContainer = mImageLoader.get(mUrl,
new ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (mErrorImageId != 0) {
setImageResource(mErrorImageId);
}
}
@Override
public void onResponse(final ImageContainer response, boolean isImmediate) {
// If this was an immediate response that was delivered inside of a layout
// pass do not set the image immediately as it will trigger a requestLayout
// inside of a layout. Instead, defer setting the image by posting back to
// the main thread.
if (isImmediate && isInLayoutPass) {
post(new Runnable() {
@Override
public void run() {
onResponse(response, false);
}
});
return;
}
if (response.getBitmap() != null) {
setImageBitmap(response.getBitmap());
} else if (mDefaultImageId != 0) {
setImageResource(mDefaultImageId);
}
}
}, maxWidth, maxHeight, scaleType);
// update the ImageContainer to be the new bitmap container.
mImageContainer = newContainer;
}
private void initBody(RelativeLayout rlBody, float ratio) {
svContent = new ScrollView(activity);
rlBody.addView(svContent, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout llContent = new LinearLayout(activity);
llContent.setOrientation(LinearLayout.HORIZONTAL);
svContent.addView(llContent, new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
etContent = new EditText(activity);
int padding = (int) (DESIGN_LEFT_PADDING * ratio);
etContent.setPadding(padding, padding, padding, padding);
etContent.setBackgroundDrawable(null);
etContent.setTextColor(0xff3b3b3b);
etContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, 21);
etContent.setText(sp.getText());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
lp.weight = 1;
llContent.addView(etContent, lp);
etContent.addTextChangedListener(this);
rlThumb = new RelativeLayout(activity);
rlThumb.setBackgroundColor(0xff313131);
int thumbWidth = (int) (DESIGN_THUMB_HEIGHT_L * ratio);
int xWidth = (int) (DESIGN_REMOVE_THUMB_HEIGHT_L * ratio);
lp = new LinearLayout.LayoutParams(thumbWidth, thumbWidth);
lp.rightMargin = lp.bottomMargin = lp.topMargin = padding;
llContent.addView(rlThumb, lp);
aivThumb = new AsyncImageView(activity) {
public void onImageGot(String url, Bitmap bm) {
thumb = bm;
super.onImageGot(url, bm);
}
};
aivThumb.setScaleToCropCenter(true);
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(thumbWidth, thumbWidth);
rlThumb.addView(aivThumb, rllp);
aivThumb.setOnClickListener(this);
initThumb(aivThumb);
xvRemove = new XView(activity);
xvRemove.setRatio(ratio);
rllp = new RelativeLayout.LayoutParams(xWidth, xWidth);
rllp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rllp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlThumb.addView(xvRemove, rllp);
xvRemove.setOnClickListener(this);
}
/**
* @param context the context to use.
* @param attrs attributes.
* @param mainView parent
* @param value value
* @param size size
* @param withLine with line.
* @param url url support.
*/
public GTextView(final Context context, AttributeSet attrs, LinearLayout mainView, String value, String size,
boolean withLine, final String url) {
super(context, attrs);
LinearLayout textLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
textLayout.setLayoutParams(layoutParams);
textLayout.setOrientation(LinearLayout.VERTICAL);
mainView.addView(textLayout);
textView = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(15, 25, 15, 15);
textView.setLayoutParams(params);
textView.setPadding(2, 2, 2, 2);
textView.setText(value);
size = size.trim();
if (size.equals("large")) {
Compat.setTextAppearance(context, textView, android.R.attr.textAppearanceLarge);
} else if (size.equals("medium")) {
Compat.setTextAppearance(context, textView, android.R.attr.textAppearanceMedium);
} else if (size.equals("small")) {
Compat.setTextAppearance(context, textView, android.R.attr.textAppearanceSmall);
} else {
int sizeInt = Integer.parseInt(size);
textView.setTextSize(sizeInt);
}
textView.setTextColor(Compat.getColor(context, R.color.formcolor));
if (url != null && url.length() > 0) {
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
}
});
}
textLayout.addView(textView);
if (withLine) {
View view = new View(context);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 2));
view.setBackgroundColor(Compat.getColor(context, R.color.formcolor));
textLayout.addView(view);
}
}
private void initTitle(RelativeLayout rlTitle, float ratio) {
tvCancel = new TextView(activity);
tvCancel.setTextColor(0xff3b3b3b);
tvCancel.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvCancel.setGravity(Gravity.CENTER);
int resId = ResHelper.getStringRes(activity, "ssdk_oks_cancel");
if (resId > 0) {
tvCancel.setText(resId);
}
int padding = (int) (DESIGN_LEFT_PADDING * ratio);
tvCancel.setPadding(padding, 0, padding, 0);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rlTitle.addView(tvCancel, lp);
tvCancel.setOnClickListener(this);
TextView tvTitle = new TextView(activity);
tvTitle.setTextColor(0xff3b3b3b);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
tvTitle.setGravity(Gravity.CENTER);
resId = ResHelper.getStringRes(activity, "ssdk_oks_contacts");
if (resId > 0) {
tvTitle.setText(resId);
}
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
rlTitle.addView(tvTitle, lp);
tvConfirm = new TextView(activity);
tvConfirm.setTextColor(0xffff6d11);
tvConfirm.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvConfirm.setGravity(Gravity.CENTER);
resId = ResHelper.getStringRes(activity, "ssdk_oks_confirm");
if (resId > 0) {
tvConfirm.setText(resId);
}
tvConfirm.setPadding(padding, 0, padding, 0);
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlTitle.addView(tvConfirm, lp);
tvConfirm.setOnClickListener(this);
}
/**
* Constructs and sets the layout parameters to have some gravity.
*
* @param gravity
* the gravity of the Crouton
* @return <code>this</code>, for chaining.
* @see android.view.Gravity
*/
public AppMsg setLayoutGravity(int gravity) {
mLayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, gravity);
return this;
}