开发者

In Android, how to re-layout views?

开发者 https://www.devze.com 2023-01-26 18:39 出处:网络
I am adding a series of custom views to a LinearLayout.I have overridden the onMeasure method of these custom vi开发者_Go百科ews, to return the dimensions based on certain parameters.Based on user inp

I am adding a series of custom views to a LinearLayout. I have overridden the onMeasure method of these custom vi开发者_Go百科ews, to return the dimensions based on certain parameters. Based on user input, I would like to change these parameters, to change the size of the views. How can I force the LinearLayout to "re-layout" it's children, to reflect the new dimensions?


Oh... linearLayout.requestLayout().


my case is I need to to relayout all children immediately. requestLayout() does not do it immediately.

For those who have the same requirement with me:

This will force relayout the view's children immediately (given that view's own width and height does not need to change)

private void reLayoutChildren(View view) {
    view.measure(
        View.MeasureSpec.makeMeasureSpec(view.getMeasuredWidth(), View.MeasureSpec.EXACTLY),
        View.MeasureSpec.makeMeasureSpec(view.getMeasuredHeight(), View.MeasureSpec.EXACTLY));
    view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
}
0

精彩评论

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