如何以编程方式将视图添加到另一个类中扩展的FrameLayout?

时间:2013-07-19 18:23:43

标签: android

我有一个创建圆圈的View类:

public class PieItem extends View{


        private final float x;
        private final float y;
        private final int r;
        private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);


        public PieItem(Context context, float x, float y, int r) {
            super(context);
            mPaint.setColor(0xFFFF0000);
            this.x = x;
            this.y = y;
            this.r = r;
        }


        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawCircle(x, y, r, mPaint);
        }


}

编辑: 我希望以编程方式将其放入framelayout中,然后在活动中重新创建它:

这是我扩展Framelayout:

 public class PieMenu extends FrameLayout{

    private Context _context;
    public PieMenu(Context context) {
        super(context);
        _context = context;
        // TODO Auto-generated constructor stub
    }


    public void addPieMenu(int x, int y){
        Toast.makeText(_context, "text",Toast.LENGTH_LONG).show();
        PieItem pieView = new PieItem(_context,x,y,1);
        FrameLayout.LayoutParams lyp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        pieView.setLayoutParams(lyp); 
        addView(pieView);
        invalidate();
    }
}

的活动:

public class MainActivity extends Activity {
    PieMenu pieMenu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pieMenu = new PieMenu(getApplicationContext());

    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int)event.getX();
        int y = (int)event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            {
                pieMenu.addPieMenu(x,y);
            }
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
        }
    return false;
    }

}

3 个答案:

答案 0 :(得分:3)

以这种方式试试你的PieMenu

public class PieMenu extends FrameLayout{

public PieMenu(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    init();

}

private void init(){

   PieItem pieView = new PieItem(context,x,y,r);

   addView(pieView);
}

答案 1 :(得分:2)

我有一个类似的视图没有被显示的问题,但是找出了原因:你需要在构造函数中添加视图(调用addView()),但是在视图完成膨胀之后(onFinishInflate()) 。这是一个示例:

public class CustomFrameLayout extends FrameLayout {
    public CustomFrameLayout(Context context) {
        super(context);
    }

    public CustomFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View blackOverlay = new View(getContext());
        blackOverlay.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        blackOverlay.setBackgroundResource(android.R.color.black);
        blackOverlay.setAlpha(0.2f);
        addView(blackOverlay);
    }
}

答案 2 :(得分:1)

你可以这样做:

    public class PieMenu extends FrameLayout {

        public PieMenu(Context context) {
            super(context);

            PieItem pieView = new PieItem(context, x, y);
            FrameLayout.LayoutParams lyp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
            pieView.setLayoutParams(lyp);
            addView(pieView);
        }
    }

您可能需要FrameLayout.LayoutParams找到PieItem,然后您可以通过PieMenu ActivityaddContentView(view, params)中添加{{1}}。