您的位置:首页 > 移动开发 > Android开发

从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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: