Android进阶资料
以下的资料是近年来,我和一些朋友面试收集整理了很多大厂的面试真题和资料,还有来自如阿里、小米、爱奇艺等一线大厂的大牛整理的架构进阶资料。希望可以帮助到大家。
Android进阶核心笔记
百万年薪必刷面试题
最全Android进阶学习视频
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
* 记录每一行的宽度,width不断取最大宽度
*/
int lineWidth = 0;
/**
* 每一行的高度,累加至height
*/
int lineHeight = 0;
int cCount = getChildCount();
// 遍历每个子元素
for (int i = 0; i < cCount; i++)
{
View child = getChildAt(i);
// 测量每一个child的宽和高
measureChild(child, widthMeasureSpec, heightMeasureSpec);
// 得到child的lp
MarginLayoutParams lp = (MarginLayoutParams) child
.getLayoutParams();
// 当前子空间实际占据的宽度
int childWidth = child.getMeasuredWidth() + lp.leftMargin
+ lp.rightMargin;
// 当前子空间实际占据的高度
int childHeight = child.getMeasuredHeight() + lp.topMargin
+ lp.bottomMargin;
/**
* 如果加入当前child,则超出最大宽度,则的到目前最大宽度给width,类加height 然后开启新行
*/
if (lineWidth + childWidth > sizeWidth-getPaddingLeft()-getPaddingRight())
{
width = Math.max(lineWidth, width);// 取最大的
lineWidth = childWidth; // 重新开启新行,开始记录
// 叠加当前高度,
height += lineHeight;
// 开启记录下一行的高度
lineHeight = childHeight;
} else
// 否则累加值lineWidth,lineHeight取最大高度
{
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, childHeight);
}
// 如果是最后一个,则将当前记录的最大宽度和当前lineWidth做比较
if (i == cCount - 1)
{
width = Math.max(width, lineWidth);
height += lineHeight;
}
}
setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth
: width, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight
: height);
}
首先得到其父容器传入的测量模式和宽高的计算值,然后遍历所有的childView,使用measureChild方法对所有的childView进行测量。然后如果父ViewGroup的宽和高设置为wrap\_content,我们通过测量计算得到所有childView的宽和高。最后根据测量模式,如果是MeasureSpec.EXACTLY则直接使用父ViewGroup传入的宽和高,否则设置为上面自己计算的宽和高。
其中第48行是考虑到假如我们父ViewGroup使用了padding这一属性的话,那么我们就得减去这个值。
第50行,原文中写的是 width = Math.max(lineWidth, childWidth);// 取最大的 这样会有个问题,假如第一行最宽,后面逐渐变小,那么我们取的width也就有问题了,故修改过来。
还有第63到第67行,因为在计算最后一个控件时,不管换行不换行,我们都没有比较最后一个控件的宽(假如没换行)和将最后一个控件的高度加上去(假如是换行了)
**4.onLayout()方法**
onLayout中完成对所有childView的位置以及大小的指定
/**
* 存储所有的View,按行记录
*/
private List<List<View>> mAllViews = new ArrayList<List<View>>();
/**
* 记录每一行的最大高度
*/
private List<Integer> mLineHeight = new ArrayList<Integer>();
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
mAllViews.clear();
mLineHeight.clear();
int width = getWidth();
int lineWidth = 0;
int lineHeight = 0;
// 存储每一行所有的childView
List<View> lineViews = new ArrayList<View>();
int cCount = getChildCount();
// 遍历所有的孩子
for (int i = 0; i < cCount; i++)
{
View child = getChildAt(i);
MarginLayoutParams lp = (MarginLayoutParams) child
.getLayoutParams();
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
// 如果已经需要换行
if (childWidth + lp.leftMargin + lp.rightMargin + lineWidth > width-getPaddingLeft()-getPaddingRight())
{
// 记录这一行所有的View以及最大高度
mLineHeight.add(lineHeight);
// 将当前行的childView保存,然后开启新的ArrayList保存下一行的childView
mAllViews.add(lineViews);
lineWidth = 0;// 重置行宽
lineViews = new ArrayList<View>();
}
/**
* 如果不需要换行,则累加
*/
lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
lineHeight = Math.max(lineHeight, childHeight + lp.topMargin
+ lp.bottomMargin);
lineViews.add(child);
}
// 记录最后一行
mLineHeight.add(lineHeight);
mAllViews.add(lineViews);
int left = getPaddingLeft();
int top = getPaddingTop();
// 得到总行数
int lineNums = mAllViews.size();
for (int i = 0; i < lineNums; i++)
{
// 每一行的所有的views
lineViews = mAllViews.get(i);
// 当前行的最大高度
lineHeight = mLineHeight.get(i);
Log.e(TAG, "第" + i + "行 :" + lineViews.size() + " , " + lineViews);
Log.e(TAG, "第" + i + "行, :" + lineHeight);
// 遍历当前行所有的View
for (int j = 0; j < lineViews.size(); j++)
{
View child = lineViews.get(j);
if (child.getVisibility() == View.GONE)
{
continue;
}
MarginLayoutParams lp = (MarginLayoutParams) child
.getLayoutParams();
//计算childView的left,top,right,bottom
int lc = left + lp.leftMargin;
int tc = top + lp.topMargin;
int rc =lc + child.getMeasuredWidth();
int bc = tc + child.getMeasuredHeight();
Log.e(TAG, child + " , l = " + lc + " , t = " + t + " , r ="
+ rc + " , b = " + bc);
child.layout(lc, tc, rc, bc);
left += child.getMeasuredWidth() + lp.rightMargin
+ lp.leftMargin;
}
left = getPaddingLeft();
top += lineHeight;
}
}
allViews的每个Item为每行所有View的List集合。
mLineHeight记录的为每行的最大高度。
23-48行,遍历所有的childView,用于设置allViews的值,以及mLineHeight的值。
57行,根据allViews的长度,遍历所有的行数
67-91行,遍历每一行的中所有的childView,对childView的left , top , right , bottom 进行计算,和定位。
92-93行,重置left和top,准备计算下一行的childView的位置。
好了,到此完成了所有的childView的绘制区域的确定,到此,我们的FlowLayout的代码也结束了
由于考虑到了padding,第32行要减去padding的大小,53、54行初始的left和top值也要相应的变化,同理第92行
其中第28-29行,利用的是view.getMeasureWidth()方法,为什么不利用view.getWidth()方法呢,其实在这个例子中,你用这个也没什么影响。但是最好是选择前者,其中下图是两者的最直接的区别的显示,我就不解释了。

左边是getHeight,右边是getMeasureHeight
**5.布局文件如下**
<LinearLayout 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="com.example.mydemo2.MainActivity" >
<com.example.mydemo2.MyFlowLayout
android:id="@+id/myFlowLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="Hello" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="Android" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="我是FlowLayout" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="一个" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="自定义的ViewGroup" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="Hello" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="Android" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="我是FlowLayout" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="一个" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="自定义的ViewGroup" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="Hello" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="Android" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="我是FlowLayout" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="一个" />
<TextView
style="@style/MyFlowLayout_viewStyle"
android:text="自定义的ViewGroup" />
</com.example.mydemo2.MyFlowLayout>
其中用到了style文件,节省了我们重复定义的时间,要修改时,我们只用修改这一个就好
style.xml
## 最后
如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

欢迎大家一起交流讨论啊~
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/topics/618156601)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
Layout_viewStyle"
android:text="自定义的ViewGroup" />
</com.example.mydemo2.MyFlowLayout>
</LinearLayout>
其中用到了style文件,节省了我们重复定义的时间,要修改时,我们只用修改这一个就好
style.xml
最后
如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。
[外链图片转存中…(img-YvDzeKnH-1715016052230)]
欢迎大家一起交流讨论啊~
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!