自定义三个View,都是继承ViewGrop,写法如下方法:
public class ViewGroupB extends ViewGroup {
public ViewGroupB( Context context ) {
super( context );
}
public ViewGroupB( Context context, AttributeSet attrs ) {
super( context, attrs );
}
public ViewGroupB( Context context, AttributeSet attrs, int defStyle ) {
super( context, attrs, defStyle );
}
@Override
protected void onLayout( boolean changed, int l, int t, int r, int b ) {
}
@Override
public boolean dispatchTouchEvent( MotionEvent ev ) {
Log.d( "TAG", "dispatchTouchEvent B : " + ev.getAction() );
return super.dispatchTouchEvent( ev );
}
@Override
public boolean onInterceptTouchEvent( MotionEvent ev ) {
Log.d( "TAG", "onInterceptTouchEvent B : " + ev.getAction() );
return super.onInterceptTouchEvent( ev );
}
@Override
public boolean onTouchEvent( MotionEvent event ) {
Log.d( "TAG", "onTouchEvent B : " + event.getAction() );
return super.onTouchEvent( event );
}
}
添加布局:
<RelativeLayout 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" >
<com.example.tempdemov.ViewGroupA
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000" >
<com.example.tempdemov.ViewGroupB
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#00ff00" >
<com.example.tempdemov.ViewGroupC
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#0000ff" >
</com.example.tempdemov.ViewGroupC>
</com.example.tempdemov.ViewGroupB>
</com.example.tempdemov.ViewGroupA>
</RelativeLayout>
效果,
这样做只是想看看View的点击事件是怎样处理的,没想到这被卡住了,懂得伙伴们帮忙看看,万分感谢。