栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android ViewGroup介绍+实例,大厂架构师经验分享

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android ViewGroup介绍+实例,大厂架构师经验分享

//每一行的高度,累加至height

int lineHeight = 0;

int count = getChildCount();

int left = getPaddingLeft();

int top = getPaddingTop();

// 遍历每个子元素

for (int i = 0; i < count; i++) {

View child = getChildAt(i);

if (child.getVisibility() == GONE)

continue;

// 测量每一个child的宽和高

measureChild(child, widthMeasureSpec, heightMeasureSpec);

// 得到child的lp

ViewGroup.LayoutParams lp = child.getLayoutParams();

// 当前子空间实际占据的宽度

int childWidth = child.getMeasuredWidth() + childHorizontalSpace;

// 当前子空间实际占据的高度

int childHeight = child.getMeasuredHeight() + childVerticalSpace;

if (lp != null && lp instanceof MarginLayoutParams) {

MarginLayoutParams params = (MarginLayoutParams) lp;

childWidth += params.leftMargin + params.rightMargin;

childHeight += params.topMargin + params.bottomMargin;

}

//如果加入当前child,则超出最大宽度,则的到目前最大宽度给width,类加height 然后开启新行

if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight()) {

width = Math.max(lineWidth, childWidth);// 取最大的

lineWidth = childWidth; // 重新开启新行,开始记录

// 叠加当前高度,

height += lineHeight;

// 开启记录下一行的高度

lineHeight = childHeight;

child.setTag(new Location(left, top + height, childWidth + left - childHorizontalSpace, height + child.getMeasuredHeight() + top));

} else {

// 否则累加值lineWidth,lineHeight取最大高度

child.setTag(new Loca

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

tion(lineWidth + left, top + height, lineWidth + childWidth - childHorizontalSpace + left, height + child.getMeasuredHeight() + top));

lineWidth += childWidth;

lineHeight = Math.max(lineHeight, childHeight);

}

}

width = Math.max(width, lineWidth) + getPaddingLeft() + getPaddingRight();

height += lineHeight;

sizeHeight += getPaddingTop() + getPaddingBottom();

height += getPaddingTop() + getPaddingBottom();

setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : width, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : height);

}

public class Location {

public Location(int left, int top, int right, int bottom) {

this.left = left;

this.top = top;

this.right = right;

this.bottom = bottom;

}

public int left;

public int top;

public int right;

public int bottom;

}

//计算当前View以及子View的位置

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

MLog.e(getClass().getName(),“onLayout”);

//获取子View个数

int count = getChildCount();

for (int i = 0; i < count; i++) {

//获取子View

View child = getChildAt(i);

//判断是否显示

if (child.getVisibility() == GONE)

continue;

//获取子View的坐标

Location location = (Location) child.getTag();

//设置子View位置

child.layout(location.left, location.top, location.right, location.bottom);

}

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

MLog.e(getClass().getName(),“onSizeChanged”);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

MLog.e(getClass().getName(),“onDraw”);

}

}

2.使用自定义CustomLayout

xmlns:custom=“http://schemas.android.com/apk/res-auto”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_margin="@dimen/dimen_20"

custom:horizontalSpace=“10dp”

custom:verticalSpace=“20dp”>

android:text=“破阵子·为陈同甫赋壮词以寄” />

android:text=“宋·辛弃疾” />

android:text=“醉里挑灯看剑” />

android:text=“梦回吹角连营” />

android:text=“八百里分麾下炙” />

android:text=“五十弦翻塞外声” />

android:text=“沙场秋点兵” />

android:text=“马作的卢飞快” />

android:text=“弓如霹雳弦惊(增加点长度)” />

android:text=“了却君王天下事” />

android:text=“赢得生前身后名” />

android:text=“可怜白发生!” />

3.自定义属性

在app/src/main/res/values/attrs.xml中添加属性

4.使用自定义属性
  • 在xml中使用

一定要添加:xmlns:test=”http://schemas.android.com/apk/res-auto”添加之后才能在xml中自定义属性,如下代码:

xmlns:custom=“http://schemas.android.com/apk/res-auto”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_margin="@dimen/dimen_20"

custom:horizontalSpace=“10dp”

custom:verticalSpace=“20dp”>

  • 在代码中使用

TypedArray attrArray = context.obtainStyledAttributes(attrs, R.styleable.CustomLayout);

if (attrArray != null) {

//参数1:获取xml中设置的参数;参数2:获取失败2使用参数作为默认值

childHorizontalSpace = attrArray.getDimensionPixelSize(R.styleable.CustomLayout_horizontalSpace, 12);

childVerticalSpace = attrArray.getDimensionPixelSize(R.styleable.CustomLayout_verticalSpace, 12);

MLog.e(getClass().getName(),“HorizontalSpace:”+childHorizontalSpace+"|VerticalSpace:"+childVerticalSpace);

//TypedArray对象池的大小默认为5,使用时记得调用recyle()方法将不用的对象返回至对象池来达到重用的目的。

attrArray.recycle();

}

写到这里自定义ViewGroup基本完成。

ViewGroup属性


ViewGroup的XML属性以及相关方法

XML属性

相关方法

说明

android:addStatesFromChildren

设置此 ViewGroup 的可绘制状态是否还包括其子级的可绘制状态。

android:alwaysDrawnWithCache

定义 ViewGroup 是否应始终使用其绘图缓存绘制其子项。

android:animateLayoutChanges

setLayoutTransition
(LayoutTransition)

定义布局更改(由添加和删除项目引起)是否应导致 LayoutTransition 运行。

android:animationCache

定义布局动画是否应为其子项创建绘图缓存。

android:clipChildren

setClipChildren(boolean)

定义孩子是否被限制在其边界内绘制。

android:clipToPadding

setClipToPadding(boolean)

如果填充不为零,则定义 ViewGroup 是否将裁剪其子项并将任何 EdgeEffect 调整大小(但不裁剪)到其填充。

android:descendantFocusability

定义 ViewGroup 和它的后代在寻找一个 View 来获得焦点时的关系。

android:layoutAnimation

定义第一次布局 ViewGroup 时使用的布局动画。

android:layoutMode

setLayoutMode(int)

定义此 ViewGroup 的布局模式。

android:persistentDrawingCache

定义绘图缓存的持久性。

android:splitMotionEvents

setMotionEventSplittingEnabled
(boolean)

设置此 ViewGroup 是否应在触摸事件调度期间拆分 MotionEvents 以分隔子视图。

ViewGroup.LayoutParams

LayoutParams 被视图用来告诉他们的父母他们想要如何布局。

基本的 LayoutParams 类只是描述了视图的宽度和高度的大小。对于每个维度,它可以指定以下之一:

1.MATCH_PARENT,这意味着视图希望与其父视图一样大(减去填充)

2.WRAP_CONTENT,这意味着视图希望足够大以包含其内容(加上填充)

3.确切的数字

ViewGroup的不同子类都有LayoutParams的子类。例如,LinearLayout有自己的 LayoutParams子类.

ViewGroup.LayoutParams(子组件)的XML属性

XML属性

说明

android:layout_height

指定视图的基本高度。

android:layout_width

指定视图的基本宽度。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/658863.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号