自定义viewGroup

代码、

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class XiaGroup extends ViewGroup {

    int groupWidth, groupHeight;
    private static final String TAG = "XiaGroup";

    public XiaGroup(Context context) {
        super(context);
    }

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

    //计算childView的测量值以及模式 ,以及设置自己的宽和高
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        switch (mode) {
            case MeasureSpec.EXACTLY:
                Log.i(TAG, "onMeasure: ");
                groupWidth = size;
                break;
            case MeasureSpec.AT_MOST:
                Log.i(TAG, "onMeasure: 111");
                groupWidth = 50;
                break;
        }
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        switch (heightMode) {
            case MeasureSpec.EXACTLY:
                Log.i(TAG, "onMeasure: ");
                groupHeight = heightSize;
                break;
            case MeasureSpec.AT_MOST:
                Log.i(TAG, "onMeasure:11122 ");
                groupHeight = 50;
                break;
        }
        setMeasuredDimension(groupWidth,groupHeight);
    }


    //子控件位置
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCountWidth = 0;
        int left = 0;
        //定义布局
        int childCont = getChildCount();
        for (int i = 0; i < childCont; i++) {
            View childAt = getChildAt(i);

            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) childAt.getLayoutParams();
            childCountWidth += layoutParams.width;
            Log.i(TAG, "onLayout: "+layoutParams.width);
            Log.i(TAG, "onLayout: "+layoutParams.height);

            int width = getWidth();
            Log.i(TAG, "onLayout: "+width);

            if (width > childCountWidth){
                Log.i(TAG, "onLayout: 横排");
                childAt.layout(left,0,layoutParams.width+left,layoutParams.height);
                left +=layoutParams.width;
            }else {
                Log.i(TAG, "onLayout: 竖排");
                childAt.layout(0,layoutParams.height,layoutParams.width,layoutParams.height*2);
            }
        }
    }

    //实现布局的属性
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LinearLayout.LayoutParams(getContext(),attrs);
    }
}

布局.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.example.viewgroup.XiaGroup
        android:layout_width="300dp"
        android:background="@color/colorPrimaryDark"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@color/colorAccent"
            android:text="真不错"/>
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@color/colorAccent"
            android:text="这是什么"/>

    </com.example.viewgroup.XiaGroup>
</LinearLayout>

结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值