Configure runtime build options

In the fragment view, I would like to adjust the layout settings of one of the child views based on the width of the containing view whose layout settings are set to match_parent. I need to do this to call getWidth / getHeight for the parent, and then set the child view to ViewGroup.LayoutParameters, etc.

The problem is that getWidth will only return a valid value after the layout was first created, and I'm not sure if there are any fragment overrides that I can use for this. For example, the actual width of a view is not determined when calling onCreateView or onStart.

So, is there an easy way to implement this, or will I have to subclass the view and override onLayout or onMeasure?

+4
source share
1 answer

You can set the global layout receiver in the view tree and do your own thing there.

final ViewTreeObserver vto = myView.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { // do layout tweaking based on measurements // remove the listener... or we'll be doing this a lot. vto.removeGlobalOnLayoutListener(this); } } 
+8
source

Source: https://habr.com/ru/post/1394830/


All Articles