3
我想使用一個ViewGroup類並添加視圖到它。那麼我們如何才能做到這一點?如何在ViewGroup中使用addView?
像這樣的東西::
public class DrawView extends View {
private static final String TAG = "DrawView";
List<Point> points = new ArrayList<Point>();
Paint paint = new Paint();
public DrawView(Context context) { //2
super(context);
System.out.println("drawwwwwwwwwwwwwwwwwwwwwconnnnnnnnnnnnn");
setFocusable(true);
setFocusableInTouchMode(true);
paint.setColor(Color.MAGENTA);
paint.setAntiAlias(true);
}
@Override
public void onDraw(Canvas canvas) {
System.out.println("drawwwwwwwwwwwwwwwwwwwww");
for (Point point : points) {
canvas.drawCircle(point.x, point.y, 5, paint);
// Log.d(TAG, "Painting: "+point);
}
}
ViewGrp ::
public class Group extends ViewGroup{
DrawView draw;
SecondView sec;
public Group(Context context) {
super(context);
// TODO Auto-generated constructor stub
draw=new DrawView(getContext());
sec=new SecondView(getContext());
addView(draw);
addView(sec);
System.out.println("hellloooooooo"); //4
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
System.out.println("hiiiiiiiiii layoutttttttttt"); //6
}
}
主要活動:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("222222222222222222222"); //1
grp=new Group(this);
setContentView(grp);
System.out.println("1111111111111111111"); //5
}
第二種觀點::
public class SecondView extends View {
private static final String TAG = "DrawView2";
Paint paint = new Paint();
public SecondView(Context context) { //3
super(context);
System.out.println("secccccccccccccccccccccccccccccccconnnnnnnn");
setFocusable(true);
setFocusableInTouchMode(true);
paint.setColor(Color.MAGENTA);
paint.setAntiAlias(true);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
// Log.d(TAG, "Painting: "+point);
System.out.println("seccccccccccccccc1");
}
請指導我寫addView在正確的地方
感謝
斯納
ViewGroup.addView(view)我沒有在這裏使用任何LayoutParams ...不能這樣做? – Smitha
我想知道如何控制流程或從哪裏開始在ViewGroup執行,就像它從onDraw() – Smitha