<?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. HUST 1339 Reversal(字符串)

    题目链接 题解:将每个单词倒置,可以用char数组,然后用空格分隔,这里用的是string和stringstream. #include <cstdio> #include <ios ...

  2. java中的字符编码方式

    1. 问题由来 面试的时候被问到了各种编码方式的区别,结果一脸懵逼,这个地方集中学习一下. 2. 几种字符编码的方式 1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符 ...

  3. BestCoder Round #85 A B C

    本来没有写博客的打算,可是看完了题解感觉这三道题这么水,我却只做出来一道,实在不应该,还是写点东西吧…… A.sum 问题描述 给定一个数列,求是否存在连续子列和为m的倍数,存在输出YES,否则输出N ...

  4. 网页 JavaScript

    今天,我主要学习了JavaScript的部分内容. JavaScript在html中的位置主要有三个地方:head里面.body里面.</html>之后,我们一般写在</html&g ...

  5. ORACLE小工具:存储过程清空所有表或使所有触发器失效

    清空所有表: CREATE OR REPLACE PROCEDURE CLEAN_TABLES as v_tablename varchar2(256); cursor cur_tablename i ...

  6. angular.js——小小记事本3

    app.js部分,首先是路由.这个之前讲过了,链接在这里—— http://www.cnblogs.com/thestudy/p/5661556.html var app = angular.modu ...

  7. 8636 跳格子(dfs+记忆化搜索)

    8636 跳格子 该题有题解 时间限制:2457MS  内存限制:1000K提交次数:139 通过次数:46 题型: 编程题   语言: G++;GCC Description 地上有一个n*m 的数 ...

  8. Shell变量:Shell变量的定义、删除变量、只读变量、变量类型

    http://c.biancheng.net/cpp/shell/ 1.打印 2.运算符

  9. HDU 1969 Pie

    二分答案+验证(这题精度卡的比较死) #include<stdio.h> #include<math.h> #define eps 1e-7 ; double a[ff]; d ...

  10. python selenium基本

    基本 from selenium import webdriver import re driver = webdriver.Firefox() driver.get('https://www.goo ...