<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="RatioLayout">
<attr name="picRatio" format="float" />
<attr name="relative">
<enum name="width" value="0" />
<enum name="height" value="1" />
</attr>
</declare-styleable> </resources>

上面分别写了两种典型的XML属性的配置,需要在values-attrs文件中编写。

第一种就是name,后面直接跟一个类型,这样可以在XML中自己任意配置。

第二种是枚举,这样就只能选择固定的属性了。

那么,问题来了,如何在View中对这些属性读取呢??下面介绍两种方法。

1、

        <com.itheima.googleplay_8.views.RatioLayout
xmlns:itheima="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
itheima:relative="width"
itheima:picRatio="2.43" >
    public RatioLayout(Context context, AttributeSet attrs) {
super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioLayout); mPicRatio = typedArray.getFloat(R.styleable.RatioLayout_picRatio, 0); mRelative = typedArray.getInt(R.styleable.RatioLayout_relative, RELATIVE_WIDTH); typedArray.recycle();
}

2、

    <com.example.myview.MyView
android:id="@+id/myView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
yueqing:content="内容"
yueqing:title="标题" >
</com.example.myview.MyView>
public MyView(Context context, AttributeSet attrs) {
super(context, attrs); View inflate = View.inflate(context, R.layout.myview, null);
CheckBox cb = (CheckBox) inflate.findViewById(R.id.cb);
TextView tv = (TextView) inflate.findViewById(R.id.tv);
String content = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/com.example.myview",
"content");
if (!cb.isChecked()) {
tv.setText(content);
} this.addView(inflate);
int attributeCount = attrs.getAttributeCount();
System.out.println(attributeCount + ">>>>>>>>>>>>>>>>");
}

Android自定义XML属性的更多相关文章

  1. Android自定义XML属性以及遇到的命名空间的问题

    转载请注明出处:http://www.cnblogs.com/kross/p/3458068.html 最近在做一些UI,很蠢很蠢的重复写了很多代码,比如一个自定义的UI Tab,由一个ImageVi ...

  2. Android--应用开发3(Android layout XML属性)

    Android layout XML属性 转载:http://www.cnblogs.com/playing/archive/2011/04/07/2008620.html Layout对于迅速的搭建 ...

  3. android自定义视图属性(atts.xml,TypedArray)学习

    是一个用于存放恢复obtainStyledAttributes(AttributeSet, int[], int, int)或 obtainAttributes(AttributeSet, int[] ...

  4. android自定义view属性

    第一种 /MainActivity/res/values/attrs.xml <?xml version="1.0" encoding="utf-8"?& ...

  5. android 自定义View属性

    在android开发过程中,用到系统的View时候可以通过XML来定义一些View的属性.比如ImageView:   android:src  和android:scaleType为ImageVie ...

  6. android AndroidManifest.xml 属性详细解析

    一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...

  7. 【Android UI】Android Layout XML属性

    Layout对于迅速的搭建界面和提高界面在不同分辨率的屏幕上的适应性具有很大的作用.这里简要介绍Android的Layout和研究一下它的实现. Android有Layout:FrameLayout, ...

  8. Android Layout XML属性

    转载自并做少量添加:http://www.cnblogs.com/playing/archive/2011/04/07/2008620.html Layout对于迅速的搭建界面和提高界面在不同分辨率的 ...

  9. android shape.xml 属性详解

    转载源:http://blog.csdn.net/harvic880925/article/details/41850723 一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标 ...

随机推荐

  1. LeetCode OJ 85. Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...

  2. [ An Ac a Day ^_^ ] FZU 2030 括号问题 搜索

    FZU一直交不上去 先写在这吧 #include<stdio.h> #include<iostream> #include<algorithm> #include& ...

  3. table边框1px

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. idea 注册码

    生成地址:http://idea.lanyus.com/ ------------------------------------- IntelliJ IDEA 注册码 *.lanyus.com及*. ...

  5. Openjudge-计算概论(A)-鸡尾酒疗法

    描述: 鸡尾酒疗法,原指“高效抗逆转录病毒治疗”(HAART),由美籍华裔科学家何大一于1996年提出,是通过三种或三种以上的抗病毒药物联合使用来治疗艾 滋病.该疗法的应用可以减少单一用药产生的抗药性 ...

  6. Apache+Tomcat服务器集群配置

    在实际应用中,如果网站的访问量很大,为了提高访问速度,可以与多个Tomcat服务器与Apache服务器集成,让他们共同运行servlet/jsp 组件的任务,多个Tomcat服务器构成了一个集群(Cl ...

  7. 搭建Mantis 缺陷管理系统

    什么是Mantis MantisBT is a free popular web-based bugtracking system (feature list). It is written in t ...

  8. simHash 简介以及java实现

    http://gemantic.iteye.com/blog/1701101 simHash 简介以及java实现 博客分类: 算法 simHash java 去重  传统的hash 算法只负责将原始 ...

  9. SSH-KeyGen 的用法

    假设 A 为客户机器,B为目标机: 要达到的目的:A机器ssh登录B机器无需输入密码:加密方式选 rsa|dsa均可以,默认dsa 做法:1.登录A机器 2.ssh-keygen -t [rsa|ds ...

  10. JavaScript shift() 方法

    http://www.w3school.com.cn/jsref/jsref_shift.asp 定义和用法 shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值. 语法 arr ...