以编程方式在android中设置“布局右侧”属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3028221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:21:57  来源:igfitidea点击:

programmatically set "Layout to right of" property in android

android

提问by Maneesh

I want to set property "Layout to right of" of control at runtime in android. Actually I want to adjust controls when screen changes orientation.

我想在运行时在 android 中设置控件的“布局右侧”属性。其实我想在屏幕改变方向时调整控件。

回答by notmystyle

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());

回答by Amir Hossein Ghasemi

If you define a new LayoutParams, you lose other rules.

如果你定义了一个 new LayoutParams,你就会失去其他规则。

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);