Is it possible to use RelativeLayout.LayoutParams to specify a bottom margin for an object? I would like to specify the bottom margin only.
try like this .
TextView mTvLine = findViewById(R.id.tv_line);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// add rule
params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.ll_status);
params.setMargins(0, 0, 0, 0);
mTvLine.setLayoutParams(params);
NOTE
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// Child widget relative to the widget: imageViewId is ABOVE of it
relativeLayoutParams.addRule(RelativeLayout.ABOVE, imageViewId.getId());
// Child widget relative to the widget: imageViewId is BELOW of it
relativeLayoutParams.addRule(RelativeLayout.BELOW, imageViewId.getId());
// Child widget relative to the widget: aligned with the bottom of the imageViewId
relativeLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, imageViewId.getId());
// The following three methods represent the same, indicating that the child widget is at the bottom of the parent widget
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1);//
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Yes is possible, but it could be an indication that what you want to do could be done in a more appropriate way. So, what's the reasoning behind this request?
In case you really need to do it programmatically instead of via xml layout, you can do it like this:
View myView = findViewById(R.layout.my_view);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
params.bottomMargin = 23; // your new value, in pixels
view.setLayoutParams(params);
Two details to be careful: margins are in pixels, but xml layouts are in dps; also, for each view type you'll need to cast to its own specific layout parameters type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.