0

我有一个创建圆圈的 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);
        }


}

编辑:我想以编程方式将其放入框架布局中,然后在活动中重新创建它:

这是我扩展框架布局:

 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;
    }

}
4

3 回答 3

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);
}
于 2013-07-19T18:29:41.200 回答
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);
    }
}
于 2015-11-09T12:58:41.370 回答
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然后您可以通过调用添加PieMenuActivityaddContentView(view, params)

于 2013-07-19T18:32:07.573 回答