简体   繁体   English

自定义视图在RelativeLayout中的位置

[英]position of custom Views within a RelativeLayout

I have a custom View: ImageZoomView.java which has a setImage, and overrides onLayout and onDraw, as following: 我有一个自定义视图:ImageZoomView.java,它具有一个setImage,并覆盖onLayout和onDraw,如下所示:

public void setImage(Bitmap bitmap) {
    mBitmap = bitmap; ...
}


@Override
protected void onDraw(Canvas canvas) {
    if (mBitmap != null && mState != null) {
        ...
        canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint);
    }
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);...
}

And i want to position several ImageZoomViews within a RelativeLayout, using layoutParams rules, but all my views are stuck to position (0,0), I don't know why. 我想使用layoutParams规则在RelativeLayout中放置几个​​ImageZoomViews,但是我所有的视图都停留在位置(0,0),我不知道为什么。

       ImageZoomView mZoomView = new ImageZoomView(getApplicationContext());

 mZoomView.setImage(mBitmap);
 mZoomView.setId(index+1);

 RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout01);
 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(screenWidth/2, screenHeight/2);
 if(index > 0)
  lp.addRule(RelativeLayout.RIGHT_OF, index);

 relativeLayout.addView(mZoomView, lp);

I've tried the exact same code with normal ImageViews, it works and every view is positionned on the right of the (n-1) view... 我用普通的ImageViews尝试了完全相同的代码,它可以正常工作,并且每个视图都位于(n-1)视图的右边...

Any ideas please? 有什么想法吗? Thanks. 谢谢。

我怀疑您的变量索引可能会保持在0值-请确保它正在递增。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM