1

这是我的主要活动:

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

}

馅饼菜单:

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,2);
        //FrameLayout.LayoutParams lyp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        //pieView.setLayoutParams(lyp); 
        addView(pieView);
        invalidate();
    }
}

PieItem.java:

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


}

MainActivity.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</FrameLayout>

每次我触摸碎石时,都会调用吐司,但不会实例化圆圈。我哪里错了?

4

3 回答 3

2

解决此问题的最简单方法,在 onCreate 中添加视图:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pieMenu = new PieMenu(getApplicationContext());

    FrameLayout fl = (FrameLayout)findViewById(R.id.layout);
    fl.addView(pieMenu);

}

并将 id 添加到您的主要活动 xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
于 2013-07-19T21:24:56.527 回答
1

You didn't add created PieMenu to activity layout. You need to call something like this in onCreate method:

view.addView(pieMenu);

Or add your PieMenu to you activity_main layout.

于 2013-07-19T20:31:35.970 回答
1

您没有添加pieMenu到布局中的任何视图 ( R.layout.activity_main)

activity_main.xml看起来像什么?

于 2013-07-19T20:30:25.593 回答