programmatically set "Layout to right of" property in android
I want to set property "Layout to right of" of control at runtime in android. Act开发者_如何学编程ually I want to adjust controls when screen changes orientation.
You can do something like this:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, view.getId());
If you define a new LayoutParams
, you lose other rules.
So if you just want to change or add a rule, do this:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
params.addRule(RelativeLayout.RIGHT_OF, targetView.getId());
view.setLayoutParams(params);
精彩评论