如何在Frame布局中实例化视图?

时间:2013-07-19 20:05:21

标签: android android-canvas android-framelayout

这是我的MainActivity:

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

}

PieMenu:

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>

每次我触摸scree时,都会调用toast但是圆圈没有被实例化。我哪里错了?

3 个答案:

答案 0 :(得分: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" >

答案 1 :(得分:1)

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

activity_main.xml看起来像什么?

答案 2 :(得分:1)

您没有将创建的PieMenu添加到活动布局中。你需要在onCreate方法中调用类似的东西:

view.addView(pieMenu);

或者将PieMenu添加到activity_main布局。