<?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. Qt_DX

    #ifndef MY_FRAME__HH__ #define MY_FRAME__HH__ #include <QtGui/QWidget> struct IDirect3D9; stru ...

  2. Openjudge-计算概论(A)-求特殊自然数

    描述: 一个十进制自然数,它的七进制与九进制表示都是三位数,且七进制与九进制的三位数码表示顺序正好相反.编程求此自然数,并输出显示. 输入为1时,输出此自然数的十进制表达式:输入为2时,输出此自然数的 ...

  3. C#中使用ServiceStack.Redis

    .Net操作Redis数据类型String public class DoRedisString : DoRedisBase { #region 赋值 /// <summary> /// ...

  4. 解决XCode插件在XCode6.4上失效的办法

    Xcode 6.4  解决 插件失效的方法 查看 插件目录: ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/ 邮件打开插件 ...

  5. onmousedown活用之碰撞效果

    通过绝对定位,在页面中随意位置设置两个div; 也就是说div 是拖动的框,div1和div2是被触碰的框; <!DOCTYPE html> <html> <head&g ...

  6. 纯计算监控(Pure computed observables)

    纯计算监控,在knockout 3.2.0里才有,提供了对性能和内存更好的管理.这是因为纯计算监控不包含对他的依赖的订阅.特点有: 防止内存泄漏 降低计算开销:值不再是observed,是一个不会重新 ...

  7. STM32F407的串口采用DMA收发数据

    源:STM32F407的串口采用DMA收发数据

  8. 执行 npm run update-webdriver 提示文件不能获取错误

    按照angularjs官网的入门教程中输入 npm run update-webdriver  总是提示 https://chromedriver.storage.googleapis.com/2.1 ...

  9. C++ 空类默认产生的类成员函数

    C++的空类有哪些成员函数:. 缺省构造函数.. 缺省拷贝构造函数.. 缺省析构函数.. 缺省赋值运算符.. 缺省取址运算符.. 缺省取址运算符 const.     注意:有些书上只是简单的介绍了前 ...

  10. mongoDB3--mongoDB的基本操作。

    [MongoDb探究]03-mongodb基本操作语句 标签: mongodbdb.createCollectiondb.collectionName.indb.dropDatabasedb.coll ...