8种机械键盘轴体对比
本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?
常识性的知识,自己是知道的,但是又不确定是否准确,经常会出现这种状况。So,为了避免这种尴尬的情况出现,工作中一些小的知识点,整理记录下来。
View控件INVISIBLE,GONE的区别
查看项目代码,很多布局是复用一个xml文件,导致有大量的逻辑去处理控件是否显示,VISIBLE不用多说,正常显示控件,一般不可见控件,我会选择GONE属性,但是还存在一种INVISIBLE属性,也是不可见,区别是什么呢?
源码探究
查看ViewGroup源码,如下方法,1
2
3
4
5
6
7
8
9
10protected void (int widthMeasureSpec, int heightMeasureSpec){
final int size = mChildrenCount;
final View[] children = mChildren;
for (int i = 0; i < size; ++i) {
final View child = children[i];
if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24protected void dispatchDraw(Canvas canvas){
...
for (int i = 0; i < childrenCount; i++) {
while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {
final View transientChild = mTransientViews.get(transientIndex);
if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
transientChild.getAnimation() != null) {
more |= drawChild(canvas, transientChild, drawingTime);
}
transientIndex++;
if (transientIndex >= transientCount) {
transientIndex = -1;
}
}
final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
more |= drawChild(canvas, child, drawingTime);
}
}
...
}
由上面两个源码方法可知,控件设置GONE属性,没有走measureChild(child, widthMeasureSpec, heightMeasureSpec);方法。
查看dispatchDraw(Canvas canvas);方法,可知,只有设置VISIBLE属性,才会执行drawChild(canvas, child, drawingTime); INVISIBLE与GONE均不执行此方法。
由View绘制流程可知,GONE属性未执行onMeasure()方法,所以不会占用界面空间。INVISIBLE执行了onMeasure()方法,但是没有执行onDraw()方法,所以会占用界面空间,但是不显示。
结论1
2
3INVISIBLE:不可见,保留了控件在界面的空间,相当于仍然占着坑
GONE:不可见,不保留控件在界面的空间,未占坑
ScrollView只能有一个子节点
ScrollView经常被使用在布局中以实现滑动效果,可以显示比实际界面的多的内容。在使用过程中,没注意,项目运行后,出现了ScrollView can host only one direct child异常:
查看官方文档,描述如下1
2
3
4
5
6
7
8public class ScrollView
extends FrameLayout
java.lang.Object
↳android.view.View
↳android.view.ViewGroup
↳android.widget.FrameLayout
↳android.widget.ScrollView
A view group that allows the view hierarchy placed within it to be scrolled. Scroll view may have only one direct child placed within it. To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout, and place additional views within that LinearLayout.
解决方案
如官方文档描述,子控件外部嵌套一层LinearLayout,因为ScrollView是继承FrameLayout,FrameLayout的特点是所有控件均重叠防止在左上角,如果有多个子控件在ScrollView,无法实现滑动效果。
代码动态addView
动态加载View,根据后台返回的数量来显示,for循环遍历,赋值,出现下图错误。The specified child already has a parent,You must call removeView() on the child's parent first.修改方案在错误中有提示。要防止一个View被add多次。
Resources not find1
2
3android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:339)
at android.widget.TextView.setText(TextView.java:5496)
View的setText()方法传int值被当成了ResourceID,导致此错误。
解决方案
可以拼接一个空子串,或者String.valueOf();1
2textView.setText(getNum + "");
textView.setText(String.valueOf(getNum));
总结
很不起眼的小细节,出现错误,会浪费大量的时间,精力去分析,定位。做好记录,加深记忆。争取下次犯高级错误。