zoukankan      html  css  js  c++  java
  • 从Android Launcher源码学习自定义标签

    res/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);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);

    mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
    mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
    .....
    }


    根据颜色看, 应该很清晰:
    1. 蓝色部分是自己定义的styleable和attr的名称, 代码和XML里要一致;
    2. 绿色部分是自定义的命名空间名称, 也只要上下保持一致即可;
    3. 红色部分是View所在的包名, 不允许有误.

    参考:http://blog.csdn.net/Android_Tutor/archive/2010/04/21/5508615.aspx

  • 相关阅读:
    值传递
    抽象类
    面向对象三大特征(二)--继承
    单例设计模式
    神奇的main方法详解
    面向对象的三大特征 ---- 封装
    变量、方法以及静态和非静态
    面向对象编程-类和对象
    数组
    力扣题库刷题(随时记录)
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2629921.html
Copyright © 2011-2022 走看看