android - 自定义ViewGroup布局显示不正确。
伊谢尔伦
伊谢尔伦 2017-04-17 17:12:15
[Android讨论组]
  1. 自定义三个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 );
    }
    
}
  1. 添加布局:

<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>
  1. 效果,

这样做只是想看看View的点击事件是怎样处理的,没想到这被卡住了,懂得伙伴们帮忙看看,万分感谢。

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号