1.View自定义属性
1.1 在资源文件中定义自定义参数,如:
<declare-styleable name="IRecyclerView">
<attr name="refreshHeaderLayout" format="reference" />
<attr name="loadMoreFooterLayout" format="reference" />
<attr name="refreshEnabled" format="boolean" />
<attr name="loadMoreEnabled" format="boolean" />
<attr name="refreshFinalMoveOffset" format="dimension" />
</declare-styleable>
这里的 format 有以下选项:
1. reference:参考某一资源ID 如@color/white
2. color:颜色值
3. boolean:布尔值
4. dimension:尺度值
5. float:浮点值
6. integer:整型值
7. string:字符串
8. fraction:百分数 如 20%
9. enum:枚举值
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
10. flag:位或运算
如以下定义:
<declare-styleable name="名称">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
<flag name = "stateVisible" value = "4" />
<flag name = "stateAlwaysVisible" value = "5" />
<flag name = "adjustUnspecified" value = "0x00" />
<flag name = "adjustResize" value = "0x10" />
<flag name = "adjustPan" value = "0x20" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable>
1.2 在代码中定义如:
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IRecyclerView, defStyle, 0);
@LayoutRes int refreshHeaderLayoutRes = -1;
@LayoutRes int loadMoreFooterLayoutRes = -1;
int refreshFinalMoveOffset = -1;
boolean refreshEnabled;
boolean loadMoreEnabled;
try {
refreshEnabled = a.getBoolean(R.styleable.IRecyclerView_refreshEnabled, false);
loadMoreEnabled = a.getBoolean(R.styleable.IRecyclerView_loadMoreEnabled, false);
refreshHeaderLayoutRes = a.getResourceId(R.styleable.IRecyclerView_refreshHeaderLayout, -1);
loadMoreFooterLayoutRes = a.getResourceId(R.styleable.IRecyclerView_loadMoreFooterLayout, -1);
refreshFinalMoveOffset = a.getDimensionPixelOffset(R.styleable.IRecyclerView_refreshFinalMoveOffset, -1);
} finally {
a.recycle();
}
特别说明
getDimension() 返回float型px值 精确
getDimensionPixelOffset() 返回int型px值 直接把小数删除
getDimensionPixelSize() 返回int型px值 进行四舍五入
2. View的位置关系
2.1. 视图的left , top , right , bottom 的值是针对其父视图的相对位置;
right = left + width;
bottom = top + height;
x=getleft+translationX;
y=gettop+translationY; //都是相对于父控件
translationX和translationY为控件的位移偏量,translationX是相对于getleft()的偏移量,translationY是相对于gettop()的偏移量 。在默认情况下translationX,translationY等于0
2.2.在MotionEvent当中,消费触摸时间当中
2.2.1 getX()和getY():由这两个函数获得的x,y值是相对的坐标值,相对于消费这个事件的视图的左上点的坐标。
2.2.2 getRawX()和getRawY():有这两个函数获得的x,y值是绝对坐标,是相对于屏幕的。
2.3 View的一些位置的方法
2.3.1 一个控件在其父窗口中的坐标位置
View.getLocationInWindow(int[] location)
2.3.2 一个控件在其整个屏幕上的坐标位置
View.getLocationOnScreen(int[] location)
2.3.3 获取视图在屏幕坐标中的可视区域
getGlobalVisibleRect(Rect r)
getGlobalVisibleRect(Rect r, Point gobalOffset)
2.3.4 获取视图本身可见的坐标区域,坐标以自己的左上角为原点(0,0)
getLocalVisibleRect(Rect r)