Set programmatic field values ​​on user view in FrameLayout

I define the view. This view contains an image, and the user can interact with it. I define FrameLayout in my main layout, and I add this view programmatically to this FrameLayout.

In the View constructor, I define MarginLayutParams to place this view in the center of the screen. Code:

mLayoutParams = new MarginLayoutParams(picture.getWidth(), picture.getHeight); mLayoutParams.setMargins(toCenterX, toCenterY, 0, 0); setLayoutParams(mLayoutParams); 

The field values ​​do not work ... The view correctly changes to the width and height, but the view does not scale to the center of the view according to the field values ​​...

Edit:

Schemes of my views:

  -> View1 (i want sclae this view to the center) Activity -> FrameLayout -> View2 (i want scale this view under the view 1) -> View3 (i want scale this view at the top right) 

Here is a sample of my ViewGroup. I am implementing a ViewGroup to manage all kinds of:

 public class MainView extends ViewGroup { public BackgroundView mBackgroundWheelArea; public BackgroundView mBackgroundLabelArea; public WheelCoreView wheelCoreArea; public LabelView labelArea; public WheelOfFortuneActivity mActivity; public MainView(Context pContext) { super(pContext); mActivity = (WheelOfFortuneActivity) pContext; mBackgroundWheelArea = new BackgroundView(pContext, R.drawable.menu_background); wheelCoreArea = new WheelCoreView(pContext, mActivity.fromPixelToDp(mBackgroundWheelArea .getBackgroundHeight()), mActivity.fromPixelToDp(mBackgroundWheelArea .getBackgroundWidth())); labelArea = new LabelView(pContext, "Web"); mBackgroundLabelArea = new BackgroundView(pContext, R.drawable.menu_background_label); this.addView(wheelCoreArea, 0); } @Override protected void onLayout(boolean pChanged, int pL, int pT, int pR, int pB) { final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); child.layout(100, 100, 0, 0); } } @Override protected boolean drawChild(Canvas canvas, View child, long drawingTime) { child.draw(canvas); return true; } } 

But wheelCoreArea does not see movement from the upper left corner !: (

Can you help me?

Sincerely.

+4
source share
1 answer

You should override the onLayout() method and set the view boundaries manually, and not set the fields.

Call view.layout(top, left, right, bottom); Bounding in member variables.

If the size of this view also changes, you may need to override the onMeasure method.

EDIT: Try this formula to calculate point placement points.

 viewLeft = displayWidth()/2 - viewWidth/2; viewTop = displayHeight()/2 - viewHeight/2; 

Call view.layout(viewLeft , viewTop , viewLeft + viewWidth, viewTop + viewHeight);

+2
source

All Articles