下面列出了android.widget.RelativeLayout#LayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void onActionZoom(View view) {
int originalWidth = view.getWidth();
int originalHeight = view.getHeight();
long currentTime = System.nanoTime();
if (time != 0 && (currentTime - time) > 10000000) {
int leftOffset = (int) (originalWidth - (originalWidth * scaleListener.scaleFactor));
int topOffset = (int) (originalHeight - (originalHeight * scaleListener.scaleFactor));
Log.e(TAG, "Offset: " + leftOffset + "," + topOffset);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.setMargins(leftOffset, topOffset, leftOffset, topOffset);
view.setLayoutParams(layoutParams);
//Avoid showing the blanks
if (view.getX() >= 0) {
view.setX(0);
}
if (view.getX() + view.getWidth() <= getScreenWidth()) {
view.setX(getScreenWidth() - view.getWidth());
}
}
time = System.nanoTime();
}
private void applyProgressBarStyle() {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, mProgressBarHeight);
switch (mProgressBarStyle) {
case PROGRESS_BAR_STYLE_INSIDE:
lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.ptr_content);
break;
case PROGRESS_BAR_STYLE_OUTSIDE:
lp.addRule(RelativeLayout.BELOW, R.id.ptr_content);
break;
}
if (mHeaderProgressBar != null)
mHeaderProgressBar.setLayoutParams(lp);
}
@Override
public void finish(){
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(mWidth,mHeight);
if (mMarginOrientationX == MARGIN_LEFT){
layoutParams.leftMargin = mXMargin;
if (mIsAlignMenuButtonX){
layoutParams.addRule(RIGHT_OF,mMenuButton.getId());
}else {
layoutParams.addRule(ALIGN_PARENT_LEFT);
}
}else {
layoutParams.rightMargin = mXMargin;
if (mIsAlignMenuButtonX){
layoutParams.addRule(LEFT_OF,mMenuButton.getId());
}else {
layoutParams.addRule(ALIGN_PARENT_RIGHT);
}
}
if (mMarginOrientationY == MARGIN_TOP){
layoutParams.topMargin = mYMargin;
if (mIsAlignMenuButtonY){
layoutParams.addRule(BELOW,mMenuButton.getId());
}else {
layoutParams.addRule(ALIGN_PARENT_TOP);
}
}else {
layoutParams.bottomMargin = mYMargin;
if (mIsAlignMenuButtonY){
layoutParams.addRule(ABOVE,mMenuButton.getId());
}else {
layoutParams.addRule(ALIGN_PARENT_BOTTOM);
}
}
mOptionButton.setLayoutParams(layoutParams);
}
private void applyProgressBarStyle() {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, mProgressBarHeight);
switch (mProgressBarStyle) {
case PROGRESS_BAR_STYLE_INSIDE:
lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.ptr_content);
break;
case PROGRESS_BAR_STYLE_OUTSIDE:
lp.addRule(RelativeLayout.BELOW, R.id.ptr_content);
break;
}
mHeaderProgressBar.setLayoutParams(lp);
}
private void setMinSize() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().show();
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
Screen.getScreenWidth(getWindowManager()), getResources()
.getDimensionPixelSize(R.dimen.player_height));
mHeaderWrapper.setLayoutParams(param);
mVV.setLayoutParams(param);
mZoomButton.setBackgroundResource(R.drawable.screensize_zoomout_button);
mCurrentScape = OrientationHelper.PORTRAIT;
}
public void initView() {
context = getContext();
RelativeLayout.LayoutParams proParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
proParams.addRule(RelativeLayout.CENTER_IN_PARENT);
progressBar = new ProgressBar(getContext());
progressBar.setLayoutParams(proParams);
addView(progressBar, proParams);
RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageView = new ImageView(getContext());
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
addView(imageView, imgParams);
}
private void changeUILayout() {
RelativeLayout.LayoutParams params = null;
params = (RelativeLayout.LayoutParams) mDisplayImgView.getLayoutParams();
params.setMargins(0, 0, 0, 0);
params.height = 200;
mDisplayImgView.setLayoutParams(params);
params = (RelativeLayout.LayoutParams) mDisplayTextView.getLayoutParams();
params.setMargins(0, 0, 0, 0);
mDisplayTextView.setLayoutParams(params);
mDisplayTextView.setTextSize(22.0f);
mContentView.invalidate();
}
/** 动态适配编辑界面的高度 */
public void run() {
int height = svContent.getChildAt(0).getHeight();
RelativeLayout.LayoutParams lp = R.forceCast(svContent.getLayoutParams());
if (height > maxBodyHeight && lp.height != maxBodyHeight) {
lp.height = maxBodyHeight;
svContent.setLayoutParams(lp);
} else if (height < maxBodyHeight && lp.height == maxBodyHeight) {
lp.height = LayoutParams.WRAP_CONTENT;
svContent.setLayoutParams(lp);
}
}
private void configureViewsVisibilityDefault() {
amountBeforeDiscount.setVisibility(GONE);
maxCouponAmount.setVisibility(GONE);
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) amountContainer.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_END);
amountContainer.setLayoutParams(params);
arrow.setVisibility(GONE);
}
/**
* Updates the icons position when called.
*/
public void updateIconDownPos() {
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(homeIcon.getWidth(), homeIcon.getHeight());
RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(galleryIcon.getWidth(), galleryIcon.getHeight());
lp1.addRule(RelativeLayout.ALIGN_PARENT_END);
lp3.addRule(RelativeLayout.ALIGN_PARENT_END);
lp1.setMargins(0, (int) (scale * (506 + iconMarginConstant - iconMarginLandscape + iconConstantSpecialCase) + 0.5f), (int) (scale * 23 + 0.5f), 0);
homeIcon.setLayoutParams(lp1);
lp3.setMargins(0, (int) (scale * (506 + iconMarginConstant - iconMarginLandscape + iconConstantSpecialCase) + 0.5f), (int) (scale * 23 + 0.5f), 0);
galleryIcon.setLayoutParams(lp3);
}
private TextView createMessageView(Context context) {
TextView view = new TextView(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
view.setLayoutParams(layoutParams);
view.setGravity(Gravity.CENTER);
view.setText(options.getMessageText());
view.setTextColor(options.getMessageColor());
view.setTextSize(TypedValue.COMPLEX_UNIT_SP, SizeUtil.textSize0_1);
return view;
}
public void onDataListChange() {
removeAllViews();
int len = mBlockListAdapter.getCount();
int w = mBlockListAdapter.getBlockWidth();
int h = mBlockListAdapter.getBlockHeight();
int columnNum = mBlockListAdapter.getCloumnNum();
int horizontalSpacing = mBlockListAdapter.getHorizontalSpacing();
int verticalSpacing = mBlockListAdapter.getVerticalSpacing();
boolean blockDescendant = getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS;
for (int i = 0; i < len; i++) {
RelativeLayout.LayoutParams lyp = new RelativeLayout.LayoutParams(w, h);
int row = i / columnNum;
int clo = i % columnNum;
int left = 0;
int top = 0;
if (clo > 0) {
left = (horizontalSpacing + w) * clo;
}
if (row > 0) {
top = (verticalSpacing + h) * row;
}
lyp.setMargins(left, top, 0, 0);
View view = mBlockListAdapter.getView(mLayoutInflater, i);
if (!blockDescendant) {
view.setOnClickListener(mOnClickListener);
}
view.setTag(INDEX_TAG, i);
addView(view, lyp);
}
requestLayout();
}
@NonNull
private ExpansionHeader createExpansionHeader() {
final ExpansionHeader expansionHeader = new ExpansionHeader(this);
expansionHeader.setBackgroundColor(Color.WHITE);
expansionHeader.setPadding(dpToPx(this, 16), dpToPx(this, 8), dpToPx(this, 16), dpToPx(this, 8));
final RelativeLayout layout = new RelativeLayout(this);
expansionHeader.addView(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //equivalent to addView(linearLayout)
//image
final ImageView expansionIndicator = new AppCompatImageView(this);
expansionIndicator.setImageResource(R.drawable.ic_expansion_header_indicator_grey_24dp);
final RelativeLayout.LayoutParams imageLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layout.addView(expansionIndicator, imageLayoutParams);
//label
final TextView text = new TextView(this);
text.setText("Trip name");
text.setTextColor(Color.parseColor("#3E3E3E"));
final RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layout.addView(text, textLayoutParams);
expansionHeader.setExpansionHeaderIndicator(expansionIndicator);
return expansionHeader;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.set_path_button:
mFontTextView.setFontPath("fonts/my_font.ttf");
break;
case R.id.create_button:
v.setEnabled(false); // can click only once
ViewGroup parent = (ViewGroup) findViewById(R.id.container_layout);
createFontTextView(parent, "fonts/my_font.ttf");
// createTextView(parent, "fonts/my_font.ttf");
break;
case R.id.replace_font_from_asset_btn:
/* Method 3: replace system default font */
FontUtils.getInstance().replaceSystemDefaultFontFromAsset(this, "fonts/my_font.ttf");
recreate();
break;
case R.id.replace_font_from_file_btn:
if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
String fontPath = Environment.getExternalStorageDirectory() + "/whinc/my_font.ttf";
FontUtils.getInstance().replaceSystemDefaultFontFromFile(this, fontPath);
recreate();
} else {
Toast.makeText(this, "External storage is not accessible!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.add_view_btn:
v.setEnabled(false); // can click only once
Button button = new Button(this, null, R.attr.buttonStyle);
button.setText("Button");
RelativeLayout layout = (RelativeLayout) findViewById(R.id.font_text_layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(button, lp);
break;
default:
break;
}
}
private static void replaceRule(View view, int oldRule, int newRule) {
RelativeLayout.LayoutParams lp = ((RelativeLayout.LayoutParams) view.getLayoutParams());
lp.addRule(oldRule, 0);
lp.addRule(newRule);
}
private void addHintView() {
if (hasAddHintView || bundle.getHintView() == null) {
return;
}
RelativeLayout.LayoutParams params = bundle.getHintViewParams() == null ? new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) : bundle.getHintViewParams();
int left, top, right, bottom;
left = top = right = bottom = 0;
int gravity = Gravity.TOP | Gravity.START;
int viewHeight = decorView.getHeight();
// below android 4.4,it can not set the status bar transparent,so we need the calculate the height of it to settle the hintView on correct position
int extraHeight = Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT ? Utils.getStatusBarHeight(getContext()) : 0;
switch (bundle.getHintViewDirection()) {
case LEFT:
gravity = Gravity.END;
top = targetViewLocation[1] + bundle.getHintViewMarginTop();
right = screenWidth - targetViewLocation[0] + bundle.getHintViewMarginRight() + bundle.getTransparentSpaceLeft() ;
break;
case RIGHT:
gravity = Gravity.START;
top = targetViewLocation[1] + bundle.getHintViewMarginTop();
left = targetViewLocation[0] + targetViewWidth + bundle.getHintViewMarginLeft() + bundle.getTransparentSpaceRight() ;
break;
case TOP:
gravity = Gravity.BOTTOM;
bottom = viewHeight - targetViewLocation[1] + bundle.getHintViewMarginBottom() + bundle.getTransparentSpaceTop();
left = targetViewLocation[0] + bundle.getHintViewMarginLeft();
break;
case BOTTOM:
gravity = Gravity.TOP;
top = targetViewLocation[1] + targetViewHeight + bundle.getHintViewMarginTop() + bundle.getTransparentSpaceBottom() - extraHeight;
left = targetViewLocation[0] + bundle.getHintViewMarginLeft();
break;
}
setGravity(gravity);
params.setMargins(left, top, right, bottom);
if (bundle.getHintView().getParent() != null) {
bundle.getHintView().setLayoutParams(params);
} else {
this.addView(bundle.getHintView(), params);
}
hasAddHintView = true;
}
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
mFragments = new ArrayList<>();
mTabLayout = findViewById(R.id.id_tabs);
mViewPager = findViewById(R.id.view_pager);
String[] tabs=getTabs();
mAdapter = new SwipeAdapter(getSupportFragmentManager(),tabs);
mViewPager.setAdapter(mAdapter);
setDefaultTab(tabs);
mViewPager.setOffscreenPageLimit(5);
mTabLayout.setupWithViewPager(mViewPager);
MusicUtils.changeTabsFont(mContext, mTabLayout);
MusicUtils.applyFontForToolbarTitle(this);
mToolbar = findViewById(R.id.toolbar);
mAppBarLayout = findViewById(R.id.id_toolbar_container);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.topMargin = Common.getStatusBarHeight(this);
mAppBarLayout.setLayoutParams(params);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(true);
mToolbar.setNavigationOnClickListener(v -> {
onPrepareOptionsMenu(mMenu);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
});
}
@Override
public View getView(Context context, final HintCase hintCase, ViewGroup parent) {
this.hintCase = hintCase;
this.parent = parent;
calculateDataToPutTheArroW(hintCase);
setArrow(context);
FrameLayout.LayoutParams frameLayoutParamsBlock = getParentLayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
gravity, marginLeft, marginTop, marginRight, marginBottom);
RelativeLayout fullBlockLayout = new RelativeLayout(context);
fullBlockLayout.setLayoutParams(frameLayoutParamsBlock);
RelativeLayout.LayoutParams relativeLayoutParamsLinear =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
for (int rule : alignBlockRules) {
relativeLayoutParamsLinear.addRule(rule);
}
relativeLayoutParamsLinear.topMargin = contentTopMargin;
relativeLayoutParamsLinear.bottomMargin = contentBottomMargin;
relativeLayoutParamsLinear.rightMargin = contentRightMargin;
relativeLayoutParamsLinear.leftMargin = contentLeftMargin;
contentLinearLayout = new LinearLayout(context);
contentLinearLayout.setBackgroundResource(R.drawable.bubble_border_background);
LayerDrawable layerDrawable = (LayerDrawable) contentLinearLayout.getBackground().getCurrent();
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.getDrawable(layerDrawable.getNumberOfLayers() - 1);
gradientDrawable.setColor(backgroundColor);
if (useBorder) {
gradientDrawable.setStroke(borderSize, borderColor);
}
contentLinearLayout.setLayoutParams(relativeLayoutParamsLinear);
contentLinearLayout.setGravity(Gravity.CENTER);
int padding = borderSize + shadowSize;
contentLinearLayout.setPadding(padding + contentLeftPadding,
padding + contentTopPadding,
padding + contentRightPadding,
padding + contentBottomPadding);
contentLinearLayout.setOrientation(LinearLayout.VERTICAL);
if (contentTitle != null) {
contentLinearLayout.addView(getTextViewTitle(context));
}
if (existImage()) {
contentLinearLayout.addView(getImage(context, imageView, imageResourceId));
}
if (contentText != null) {
contentLinearLayout.addView(getTextViewDescription(context));
}
fullBlockLayout.addView(contentLinearLayout);
fullBlockLayout.addView(arrow);
return fullBlockLayout;
}
private void setTopMargin(View v, int topMargin){
RelativeLayout.LayoutParams lp = (android.widget.RelativeLayout.LayoutParams) v.getLayoutParams();
lp.topMargin = topMargin;
v.setLayoutParams(lp);
}