Android中ViewGroup等容器控件的使用

package com.kesalin.RippleDemo;
import android.R.string;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


public class MainView extends ViewGroup{
	ImageView v1,v2;
	OnClickListener v1lClickListener = null;
	OnClickListener v2lClickListener = null;
	final String TAG = "MainView";
	public MainView(Context context) {
		super(context);
		setBackgroundResource(R.drawable.bg);
		
		Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
		v1 = new ImageView(getContext());
		v1.setImageBitmap(bmp1);
		addView(v1);
		
		v2 = new ImageView(getContext());
		v2.setImageBitmap(bmp1);
		addView(v2);
		
		v1lClickListener = new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d(TAG,"child v1 onTouch down");
			}
		};
		v2lClickListener = new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d(TAG,"child v2 onTouch down");
			}
		};
		v1.setOnClickListener(v1lClickListener);
		v2.setOnClickListener(v2lClickListener);
	}

	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();
		Log.d(TAG,"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());
		
		final View myview = getChildAt(1);
		myview.setVisibility(View.VISIBLE);
		myview.measure(r - l, b - t);
		myview.layout(10, 50, myview.getMeasuredWidth(), myview.getMeasuredHeight()+40);
   }
}

你可能感兴趣的:(Android中ViewGroup等容器控件的使用)