ViewGroup实例
view类:
package com.kx.tang.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.kx.tang.R;
public class MainView extends ViewGroup {
ImageView v1;
public MainView(Context context) {
super(context);
Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
v1 = new ImageView(getContext());
v1.setImageBitmap(bmp1);
addView(v1);
}
public MainView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MainView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
int childCount = getChildCount();
System.out.println("Child Count = " + childCount + ":tang");
final View child = getChildAt(0);
child.setVisibility(View.VISIBLE);
child.measure(r - l, b - t);
child.layout(10, 10, child.getMeasuredWidth(), child
.getMeasuredHeight());
}
}
Activity类:
package com.kx.tang;
import android.app.Activity;
import android.os.Bundle;
import com.kx.tang.view.MainView;
public class ActivityMain extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MainView(this));
}
}
ViewGroup继承了view类,跟view同属最高层的类,frameLayout等布局类都是继承了ViewGroup.所以说ViewGroup是一个了多个控件的view.其余的几个布局类都是继承了ViewGroup。