开发者

Remove rule from RelativeLayout

开发者 https://www.devze.com 2023-03-29 00:48 出处:网络
I have bookmark ImageButton with ALIGN_WITH_PARENT开发者_StackOverflow set to true in XML. If I programatically do (I want to remove that rule)

I have bookmark ImageButton with ALIGN_WITH_PARENT开发者_StackOverflow set to true in XML.

If I programatically do (I want to remove that rule)

RelativeLayout.LayoutParams params = (LayoutParams) bookmark.getLayoutParams(); 
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
bookmark.setLayoutParams(params);

The rule just doesn't apply (in my layout I can see that rule is still active) -> layout isn't good. If I manually do it (some sort of "simulation", set ALIGN WITH PARENT to false) in my Layout Editor, my layout is fine which leads to this code up there. Something is wrong.

What?


(I believe temporary) Solution is to wrap up that ImageButton in one dummy RelativeLayout and then take the params of that dummy layout and add or remove rules. That way it's working just fine.


Insead of calling setLayoutParams(), try to use requestLayout() method:

bookmark.requestLayout();

From API docs:

Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.


simply call removeRule() function on params.

for example,

params.removeRule(RelativeLayout.CENTER_IN_PARENT);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号