从Android Launcher源码学习自定义标签
2012-05-12 14:50
411 查看
es/values/attrs.xml
<declare-styleable name="CellLayout">
<!-- The width of a single cell -->
<attr name="cellWidth" format="dimension" />
<!-- The height of a single cell -->
<attr name="cellHeight" format="dimension" />
.....
</declare-styleable>
res/layout-port/workspace_screen.xml
<com.android.launcher.CellLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
launcher:cellWidth="80dip"
launcher:cellHeight="96dip"
....
/>
src/com.android.launcher.CellLayout.java
public CellLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// 通过下面这句代码,定位到Attrs中的<declare-styleable name="CellLayout">
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout,
defStyle, 0);
// 通过下面这两句话,在workspace_screen.xml中找到对应标签对应的值!
mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth,
10);
mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight,
10);
.....
}
根据颜色看, 应该很清晰:
1. 蓝色部分是自己定义的styleable和attr的名称, 代码和XML里要一致;
2. 绿色部分是自定义的命名空间名称, 也只要上下保持一致即可;
3. 红色部分是View所在的包名, 不允许有误.
============
具体还不是很清楚,比如在Android 4.0 Launcher中的PagedView中的
public PagedView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.PagedView, defStyle, 0);
mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
R.styleable.PagedView 和 R.styleable.PagedView_pageSpacing 如何与
<!-- The workspace contains 5 screens of cells -->
<com.android.launcher2.Workspace
android:id="@+id/workspace"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/qsb_bar_height_inset"
android:paddingBottom="@dimen/button_bar_height"
launcher:defaultScreen="2"
launcher:cellCountX="4"
launcher:cellCountY="4"
launcher:pageSpacing="@dimen/workspace_page_spacing"
联系在一起,workspace 与Pagedview虽然有继承关系,但是workspace并不是唯一一个继承与PagedView的类,若是Launcher.xml 文件中有多个 继承与PagedView的类的标签,那么如何区分!!!
参考:http://blog.csdn.net/Android_Tutor/archive/2010/04/21/5508615.aspx
<declare-styleable name="CellLayout">
<!-- The width of a single cell -->
<attr name="cellWidth" format="dimension" />
<!-- The height of a single cell -->
<attr name="cellHeight" format="dimension" />
.....
</declare-styleable>
res/layout-port/workspace_screen.xml
<com.android.launcher.CellLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
launcher:cellWidth="80dip"
launcher:cellHeight="96dip"
....
/>
src/com.android.launcher.CellLayout.java
public CellLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// 通过下面这句代码,定位到Attrs中的<declare-styleable name="CellLayout">
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout,
defStyle, 0);
// 通过下面这两句话,在workspace_screen.xml中找到对应标签对应的值!
mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth,
10);
mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight,
10);
.....
}
根据颜色看, 应该很清晰:
1. 蓝色部分是自己定义的styleable和attr的名称, 代码和XML里要一致;
2. 绿色部分是自定义的命名空间名称, 也只要上下保持一致即可;
3. 红色部分是View所在的包名, 不允许有误.
============
具体还不是很清楚,比如在Android 4.0 Launcher中的PagedView中的
public PagedView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.PagedView, defStyle, 0);
mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
R.styleable.PagedView 和 R.styleable.PagedView_pageSpacing 如何与
<!-- The workspace contains 5 screens of cells -->
<com.android.launcher2.Workspace
android:id="@+id/workspace"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/qsb_bar_height_inset"
android:paddingBottom="@dimen/button_bar_height"
launcher:defaultScreen="2"
launcher:cellCountX="4"
launcher:cellCountY="4"
launcher:pageSpacing="@dimen/workspace_page_spacing"
联系在一起,workspace 与Pagedview虽然有继承关系,但是workspace并不是唯一一个继承与PagedView的类,若是Launcher.xml 文件中有多个 继承与PagedView的类的标签,那么如何区分!!!
参考:http://blog.csdn.net/Android_Tutor/archive/2010/04/21/5508615.aspx
相关文章推荐
- Spring源码学习——自定义标签
- spring源码学习之:xml配置文件标签自定义
- android学习之往launcher中添加自定义页面
- 极客学院-通过"Android学习之Launcher源码学习"看UML序列图
- [RK3288][Android7.1.2] Launcher3 源码阅读之step6:根据布局文件找到对应的自定义View
- Android学习——Launcher源码在eclipse运行
- Android学习札记47:TextView显示Html类解析的网页和图片及自定义标签
- (精)Spring IOC核心源码学习III:bean标签和自定义标签实现原理
- Linphone-Android源码学习(二、LinphoneLauncherActivity)
- spring源码学习之:xml配置文件标签自定义
- Android Framework学习——Launcher启动应用程序过程源码分析
- spring源码学习之:xml配置文件标签自定义
- Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析
- 【spring源码学习】spring的IOC容器之自定义xml配置标签扩展namspaceHandler向IOC容器中注册bean
- android1.6 launcher源码学习笔记
- Android学习——Launcher源码在eclipse运行
- Spring源码解析-自定义标签解析
- 快乐分享Android学习心得---如何在Eclipse中看Android2.2的源码
- android源码 launcher之android:clearTaskOnLaunch属性
- 打造自己的Android源码学习环境之四:下载Android源代码