How do I programmatically remove an existing rule that was defined in XML?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 07:31:45
Michael

You can't remove a rule because all rules are always stored in a fixed-size java array. But you can set a rule to 0. For example

layoutParams.addRule(RelativeLayout.RIGHT_OF, 0);
layoutParams.addRule(RelativeLayout.BELOW, R.id.new_ref_LinearLayout);

EDIT (thanks to Roger Rapid):

As of API level 17, the class RelativeLayout.LayoutParams has the following method:

public void removeRule(int verb) 

So you can remove a rule using the following line of code:

layoutParams.removeRule(RelativeLayout.RIGHT_OF);

And you will get exactly the same result as when 'adding' a zero-rule as:

layoutParams.addRule(RelativeLayout.RIGHT_OF, 0);

I think you need to call:

relativeLayout.updateViewLayout(linearLayoutToMove, layoutParams);

after changing the LayoutParams.

In reply to the edit, you can create new LayoutParameters using:

LinearLayout.LayoutParams = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT,
    LinearLayout.LayoutParams.FILL_PARENT);

and then add your new rules. Then, update the layout parameters using the previously mentioned updateViewLayout() method.

add the following code to your existing code

linearLayoutToMove.setLayoutParams(layoutParams)

I think this should do the job. In case if the above line dont work, try to call linearLayoutToMove.invalidate() after the above line.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!