安卓自定义viewgroup

1.ViewGroup

前面学习的安卓view说过,我认为安卓view就如word编辑当中的文字,图像,表格等等都是一个view.那么当我们的word文字,图像有规律的摆放组合起来形成一个比较好看的排版模式的时候,我们可以保存起来并且作为样式,那么以后每次编辑的时候只需要修改对应的文字内容以及图片内容就可以了。那么如此以来,viewgroup的概念也差不多了,我理解的viewgroup就是把一些view进行组合起来形成一个可以比较固定demo样式的view组合。它的作用就是作为各个view之间可以按一定规则摆放的组合demo样式供开发人员使用。套用模快速开发并且可以循环使用。
-

2.自定义view实例

既然说viewgroup是一些view组合样式,那么肯定有一些样式是没有的,那就需要我们自己创造,所以这就是自定义viewgroup了,自定义viewgroup可以让各个子view之间按我们的意愿摆放。

实现三个textview对角线摆放
(1)MyViewGroup文件

package com.gentle.viewgroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class MyViewGroup extends ViewGroup {

    public MyViewGroup(Context context){
        super(context);

    }

    public MyViewGroup(Context context, AttributeSet attrs){
        super(context,attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int row = 0;
        int column = 0;
        int count = getChildCount();
        for(int i=0;i<count;i++){
            View child = getChildAt(i);

            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();

            //子view的布局,即排放位置
            child.layout(row,column,row+childWidth,column+childHeight);
            row += childWidth;
            column += childHeight;

        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //获取viewgroup的测量方式以及大小
        int groupWidthSize = MeasureSpec.getSize(widthMeasureSpec);
        int groupHeightSize = MeasureSpec.getSize(heightMeasureSpec);

        int groupWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int groupHeightMode = MeasureSpec.getMode(heightMeasureSpec);

        //测量子view,下面要获取子view的宽度以及长度
        measureChildren(widthMeasureSpec,heightMeasureSpec);

        int widht = 0;
        int height =0;

        if(groupWidthMode == MeasureSpec.AT_MOST){
            for (int i= 0; i < getChildCount();i++){
                View child = getChildAt(i);//获取子view
                widht +=  child.getMeasuredWidth();//获取子view大小
            }
        }else
            widht = groupWidthSize;

        if(groupHeightMode == MeasureSpec.AT_MOST){
            for(int i=0;i<getChildCount();i++){
                View child = getChildAt(i);
                height += child.getMeasuredHeight();
            }
        }else
            height = groupHeightSize;

        setMeasuredDimension(widht,height);


    }
}

(2)xml文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.gentle.viewgroup.MyViewGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorBack">

        <TextView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@color/colorRed"
            android:padding="10dp"
            android:text="hello"/>

        <TextView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@color/colorGreen"
            android:padding="10dp"
            android:text="world"/>

        <TextView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@color/colorBlue"
            android:padding="10dp"
            android:text="!"/>

    </com.gentle.viewgroup.MyViewGroup>

</androidx.constraintlayout.widget.ConstraintLayout>

(3)运行结果
在这里插入图片描述

3.添加viewgroup自定义属性以及子view间隔

主要是加入有子view的layout_position属性,就对layout_postion属性进行子view规则摆放,
layout_position为right的值的话,也就是枚举值为2,则子view就往右摆放,否则默认往左摆放。

(1)新建ViewGroup属性值,主要是枚举值

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="customLayoutPosition">
       <attr name="layout_postion">
            <enum name="left" value="1"/>
            <enum name="right" value="2"/>
       </attr>
   </declare-styleable>
</resources>

(2)MyVierGroup
主要是以下几个重要点:
*重写generateLayoutParams方法,该方法主要放回一个继承MarginLayoutParams类型的对象,例如本例子中的MyLayoutParams,重写了该方法后就子view就可以通过child.getLayoutParams获取一个MarginLayoutParams类型对象。
*在测量onMeasure方法的时候把margin参数加上
*在onLayout中对子view进行间隔摆放处理

package com.gentle.viewgroup;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.gentle.gentleviewgroup.R;

import java.util.PropertyResourceBundle;

public class MyViewGroup extends ViewGroup {

    public MyViewGroup(Context context){
        super(context);

    }

    public MyViewGroup(Context context, AttributeSet attrs){
        super(context,attrs);
    }

    //重写generateLayoutParams,主要获取子view相对于父控件的放置位置
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MyLayoutParams(getContext(),attrs);
    }

	//内部类继承MarginLayoutParams
    public static class MyLayoutParams extends MarginLayoutParams {
        private static final int POSITION_LEFT = 1;
        private static final int POSITION_RIGHT = 2;
        public int layout_position = 1;

        public MyLayoutParams(Context context, AttributeSet attrs){
            super(context,attrs);

            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.customLayoutPosition);
            layout_position =  typedArray.getInt(R.styleable.customLayoutPosition_layout_postion,0);
            typedArray.recycle();

            //默认从左边排列
            if(layout_position == 0){
                layout_position = 1;
            }

         }

        public MyLayoutParams(int width, int height){
            super(width,height);
        }

        public MyLayoutParams(LayoutParams source) {
            super(source);
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //获取viewgroup的测量方式以及大小
        int groupWidthSize = MeasureSpec.getSize(widthMeasureSpec);
        int groupHeightSize = MeasureSpec.getSize(heightMeasureSpec);

        int groupWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int groupHeightMode = MeasureSpec.getMode(heightMeasureSpec);

        //测量子view,下面要获取子view的宽度以及长度
        measureChildren(widthMeasureSpec,heightMeasureSpec);


        int widht = 0;
        int height =0;


        if(groupWidthMode == MeasureSpec.AT_MOST){
            for (int i= 0; i < getChildCount();i++){
                View child = getChildAt(i);//获取子view
                MyLayoutParams layoutParams = (MyLayoutParams)child.getLayoutParams();
				//考虑子view的间隔-----------------
                widht +=  child.getMeasuredWidth()+layoutParams.leftMargin + layoutParams.rightMargin;//获取子view大小
            }
        }else
            widht = groupWidthSize;

        if(groupHeightMode == MeasureSpec.AT_MOST){
            for(int i=0;i<getChildCount();i++){
                View child = getChildAt(i);
                MyLayoutParams layoutParams = (MyLayoutParams)child.getLayoutParams();
                //考虑子view间隔
                height += child.getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin;
            }
        }else
            height = groupHeightSize;

        setMeasuredDimension(widht,height);


    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int row = 0;
        int column = 0;
        int count = getChildCount();
        for(int i=0;i<count;i++){
            View child = getChildAt(i);

            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            
            //获取子view摆放规则
            MyLayoutParams layoutParams = (MyLayoutParams) child.getLayoutParams();
            
            //根据当前子view布局,依次靠右放置
            if(layoutParams.layout_position == MyLayoutParams.POSITION_RIGHT){
                child.layout(getWidth()-childWidth-layoutParams.rightMargin,column,getWidth()-layoutParams.rightMargin,column + childHeight);
            }else {
                //否则就默认从左斜线布局
                //子view的布局,即排放位置
                child.layout(row, column, row + childWidth, column + childHeight);
                row += childWidth+layoutParams.leftMargin + layoutParams.rightMargin;
            }
            column += childHeight +layoutParams.topMargin + layoutParams.bottomMargin;
        }
    }
}

(3)xml布局文件
通过app:layout_postion = "right"往右摆放

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.gentle.viewgroup.MyViewGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorBack">

        <TextView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@color/colorRed"
            android:padding="10dp"
            android:text="hello"
            android:layout_margin="10dp"
            app:layout_postion = "right"/>

        <TextView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@color/colorGreen"
            android:padding="10dp"
            android:layout_margin="10dp"
            android:text="world"
            app:layout_postion = "right"/>

        <TextView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@color/colorBlue"
            android:padding="10dp"
            android:layout_margin="10dp"
            android:text="!"
            app:layout_postion = "right"/>

    </com.gentle.viewgroup.MyViewGroup>



</androidx.constraintlayout.widget.ConstraintLayout>

(4)运行结果
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android自定义ViewGroup是指在Android开发,通过继承ViewGroup类来创建自定义的布局容器。自定义ViewGroup可以用于实现一些特殊的布局效果,比如侧滑菜单、滑动卡片等等。通过自定义ViewGroup,我们可以更灵活地控制子视图的布局和交互行为,以满足特定的需求。自定义ViewGroup的实现主要包括重写onMeasure()方法和onLayout()方法,来测量和布局子视图。同时,我们还可以通过重写onInterceptTouchEvent()方法和onTouchEvent()方法来处理触摸事件,实现自定义的交互效果。如果你对自定义ViewGroup还不是很了解,或者正想学习如何自定义,可以参考相关的教程和文档,如引用\[1\]和引用\[2\]所提到的博客和官方文档。 #### 引用[.reference_title] - *1* [Android 手把手教您自定义ViewGroup(一)](https://blog.csdn.net/iteye_563/article/details/82601716)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [使用LayoutParams自定义安卓ViewGroup](https://blog.csdn.net/lfq88/article/details/127268493)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Android自定义ViewGroup](https://blog.csdn.net/farsight2009/article/details/62046643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值