android – 获取对父ViewGroup的引用

android – 获取对父ViewGroup的引用,第1张

概述我通过向ViewGroup添加一个形状和句柄作为单独的视图来创建可伸缩的形状.单击处理程序后,如何获取对ViewGroup的引用以便我可以扩展所有内容? handle.getParent()返回null.我的ViewGroup是以编程方式创建的. public class ShapeView extends ViewGroup { private SelectorView mSelect @H_502_1@ 我通过向VIEwGroup添加一个形状和句柄作为单独的视图来创建可伸缩的形状.单击处理程序后,如何获取对VIEwGroup的引用以便我可以扩展所有内容? handle.getParent()返回null.我的VIEwGroup是以编程方式创建的.

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的引用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1129674.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存