public class ShapeVIEw extends VIEwGroup { private SelectorVIEw mSelectorVIEw; public ShapeVIEw (Context context) { super(context); relativeLayout.LayoutParams p = new relativeLayout.LayoutParams(200,200); this.setLayoutParams(p); mSelectorVIEw = new SelectorVIEw(context); this.addVIEw(mSelectorVIEw); }}public class SelectorVIEw extends VIEw {public RectangleDrawable mRectangleDrawable; public SelectorVIEw (Context context) { super(context); Log.v(TAG,"constructor"); mRectangleDrawable = new RectangleDrawable(); relativeLayout.LayoutParams p = new relativeLayout.LayoutParams(20,20); this.setLayoutParams(p); } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); mRectangleDrawable.draw(canvas); } public boolean ontouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { VIEwGroup parentVIEw = (VIEwGroup)this.getParent(); parentVIEw.setX(100); parentVIEw.setY(100); break; } } return true; }}解决方法 请使用SelectorVIEw.this.getParent()而不是this.getParent()
public class SelectorVIEw extends VIEw {public RectangleDrawable mRectangleDrawable;public SelectorVIEw (Context context) { super(context); Log.v(TAG,"constructor"); mRectangleDrawable = new RectangleDrawable(); relativeLayout.LayoutParams p = new relativeLayout.LayoutParams(20,20); this.setLayoutParams(p);}@OverrIDeprotected voID onDraw(Canvas canvas) { super.onDraw(canvas); mRectangleDrawable.draw(canvas); }public boolean ontouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { VIEwGroup parentVIEw = (VIEwGroup)SelectorVIEw.this.getParent(); parentVIEw.setX(100); parentVIEw.setY(100); break; } } return true; }}总结
以上是内存溢出为你收集整理的android – 获取对父ViewGroup的引用全部内容,希望文章能够帮你解决android – 获取对父ViewGroup的引用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)