zoukankan      html  css  js  c++  java
  • 安卓学习53

    今天学习了安卓的一些知识其中主要的是:

    Android 自定义View 以及以及layout 属性全攻略

    对于Android 系统的自定义View 可能大家都熟悉了,对于自定义View 的属性添加,以及Android 的Layout 的命名空

    间问题,很多网友还不是很清楚,今天Android123 一起再带大家温习一下

    CwjView myView=new CwjView(context);

    如果用于游戏或整个窗体的界面,

    我们可能直接在onCreate 中setContentView(myView); 当然如果是控件,我们可能会需要从Layout 的xml 中声明,比

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    />

    当然,我们也可以直接从父类声明比如

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    />

    上面我们仅用了父类View 的两个属性,均来自android 命名空间,而名称为layout_width 或layout_height,我们自

    定义的控件可能有更多的功能,比如

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    cwj:age="22"

    cwj:university="sjtu"

    cwj:city="shanghai"

    />

    我们可以看到上面的三个属性,是我们自定义的。

    作为标准xml 规范,可能还包含了类似

    xmlns:android="http://www.doczj.com/doc/f563ff3efe4733687e21aae8.html/apk/res/android" 这样的语句,对于定义完整的View,我们的命名空

    间为cwj,

    这里可以写为

    xmlns:cwj=http://www.doczj.com/doc/f563ff3efe4733687e21aae8.html/apk/res/http://www.doczj.com/doc/f563ff3efe4733687e21aae8.html.android123.cwjView 或

    xmlns:cwj=http://www.doczj.com/doc/f563ff3efe4733687e21aae8.html/apk/res/android 都可以。

    对于定义的cwj 命名空间和age、university 以及city 的三个属性我们如何定义呢? 在工程的res/values 目录中我们

    新建一个cwj_attr.xml 文件,编码方式为utf-8 是一个好习惯,内容如下

    这里我们可能对format 不是很熟悉,目前Android 系统内置的格式类型有integer 比如ProgressBar 的进度值,float

    比如RatingBar 的值可能是 3.5 颗星,boolean 比如ToggleButton 的是否勾选,string 比如TextView 的text 属性,

    当然除了我们常见的基础类型外,Android 的属性还有特殊的比如color 是用于颜色属性的,可以识别为#FF0000 等类

    型,当然还有dimension 的尺寸类型,比如23dip,15px,18sp 的长度单位,还有一种特殊的为reference,一般用于

    引用@+id/cwj @drawable/xxx 这样的类型。当然什么时候用reference 呢? 我们就以定义一个颜色为例子,<attr< p="">

    name="red" format="color|reference" /> 这里我们用了逻辑或的运算符,定义的红色是颜色类型的,同时可以被引

    用当然,对于我们自定义的类中,我们需要使用一个名为obtainStyledAttributes 的方法来获取我们的定义。在我

    们自定义View 的构造方法(Context context, AttributeSet attrs)的重载类型中可以用

    public CwjView(Context context, AttributeSet attrs) {

    super(context, attrs);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.cwj_attr);

    mAge = a.getInteger(R.styleable.CwjView_age, 22);

    mCity = a.getString(R.styleable.CwjView_city, "shanghai");

    mUniversity= a.getString(R.styleable.CwjView_university, "sjtu");

    a.recycle(); //Android123 提示大家不要忘了回收资源

    }

    这样类的全局成员变量mAge、mCity 就获取了我们需要的内容,当然根据layout 中的数值我们自定义的CwjView 需要

    动态的处理一些数据的情况,可以使用AttributeSet 类的getAttributeResourceValue 方法获取。

    public CwjView(Context context, AttributeSet attrs)

    {

    super(context, attrs);

    resId = attrs.getAttributeResourceValue("http://www.doczj.com/doc/f563ff3efe4733687e21aae8.html.android123.CwjView", "age", 100);

    resId = attrs.getAttributeResourceValue("http://www.doczj.com/doc/f563ff3efe4733687e21aae8.html.android123.CwjView", "city", "shanghai");

    //resID 就可以任意使用了

    }

    以上两种方法中,参数的最后一个数值为默认的,如果您有不明白的地方可以来函到android123@http://www.doczj.com/doc/f563ff3efe4733687e21aae8.html 我们会在第

    一时间回复。

  • 相关阅读:
    【转+补充】在OpenCV for Android 2.4.5中使用SURF(nonfree module)
    Delphi StarOffice Framework Beta 1.0 发布
    Angular ngIf相关问题
    angularjs文档下载
    公众号微信支付开发
    公众号第三方平台开发 教程六 代公众号使用JS SDK说明
    公众号第三方平台开发 教程五 代公众号处理消息和事件
    公众号第三方平台开发 教程四 代公众号发起网页授权说明
    公众号第三方平台开发 教程三 微信公众号授权第三方平台
    公众号第三方平台开发 教程二 component_verify_ticket和accessToken的获取
  • 原文地址:https://www.cnblogs.com/092e/p/14916702.html
Copyright © 2011-2022 走看看