Android自定义XML属性
<?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属性的更多相关文章
- Android自定义XML属性以及遇到的命名空间的问题
转载请注明出处:http://www.cnblogs.com/kross/p/3458068.html 最近在做一些UI,很蠢很蠢的重复写了很多代码,比如一个自定义的UI Tab,由一个ImageVi ...
- Android--应用开发3(Android layout XML属性)
Android layout XML属性 转载:http://www.cnblogs.com/playing/archive/2011/04/07/2008620.html Layout对于迅速的搭建 ...
- android自定义视图属性(atts.xml,TypedArray)学习
是一个用于存放恢复obtainStyledAttributes(AttributeSet, int[], int, int)或 obtainAttributes(AttributeSet, int[] ...
- android自定义view属性
第一种 /MainActivity/res/values/attrs.xml <?xml version="1.0" encoding="utf-8"?& ...
- android 自定义View属性
在android开发过程中,用到系统的View时候可以通过XML来定义一些View的属性.比如ImageView: android:src 和android:scaleType为ImageVie ...
- android AndroidManifest.xml 属性详细解析
一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...
- 【Android UI】Android Layout XML属性
Layout对于迅速的搭建界面和提高界面在不同分辨率的屏幕上的适应性具有很大的作用.这里简要介绍Android的Layout和研究一下它的实现. Android有Layout:FrameLayout, ...
- Android Layout XML属性
转载自并做少量添加:http://www.cnblogs.com/playing/archive/2011/04/07/2008620.html Layout对于迅速的搭建界面和提高界面在不同分辨率的 ...
- android shape.xml 属性详解
转载源:http://blog.csdn.net/harvic880925/article/details/41850723 一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标 ...
随机推荐
- CMake VS工程总结
1.设置输出后缀 set(CMAKE_DEBUG_POSTFIX "d") 2.设置输出目录 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_ ...
- Redis持久存储-AOF&RDB
Redis中数据存储模式有2种:cache-only,persistence;cache-only即只做为"缓存"服务,不持久数据,数据在服务终止后将消失,此模式下也将不存在&qu ...
- [ An Ac a Day ^_^ ] hdu 5835 Danganronpa 令人发指
这道题告诉我两个道理: 1.记得写case 要不挂死你 2.数据很水的时候 只有样例的这一种情况…… 原来数据可以这么水…… #include<stdio.h> #include<i ...
- As3.0 TextField
一 TextField 对象的方法 方法 说明 TextField.addListener 加入接收触发事件如文本域内容变化或滚动变化的监听对象,触发事件可以参看最后一个表. TextField.ge ...
- Python Tools
[TOC] Python virtualenv.fabric 和 pip 是 pythoneer 的三大神器 pip pip pip是一个安装和管理Python包的工具,是easy_install的一 ...
- 分珠(dfs+并查集)
1140 分珠 时间限制:500MS 内存限制:65536K提交次数:24 通过次数:18 题型: 编程题 语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不 ...
- File文件操作类
public class FileTest { //遍历出E:根目录下所有的文件夹,并输出文件夹名 static void testOne(){ //构建File对象,设置文件路径 File ro ...
- Code Blocks 多文件联合编译
code::blocks是一款据说灰常强大的IDE,以前虽然也经常使用,但一没用过高度功能,二来没用它写过工程性的东西,简单点说就是一个以上的源文件并且加入其他非标准的头文件,今天想做一个多文 ...
- OpenCV ——背景建模之CodeBook(2)
1,CodeBook的来源 先考虑平均背景的建模方法.该方法是针对每一个像素,累积若干帧的像素值,然后计算平均值和方差,以此来建立背景模型,相当于模型的每一个像素含有两个特征值,这两个特征值只是单纯的 ...
- python http请求
from httplib2 import Http def postDada(): http=Http() heads={"pragma":"no-cache" ...