Android自定义XML属性以及遇到的命名空间的问题
转载请注明出处:http://www.cnblogs.com/kross/p/3458068.html
最近在做一些UI,很蠢很蠢的重复写了很多代码,比如一个自定义的UI Tab,由一个ImageView和一个TextView构成,如果不自定义属性的话,就需要单独new出几个Tab,然后分别给它们设置Drawable和Text。如果能使用XML属性的话,就直接在XML文件中就可以给Tab设置好Drawable和Text。Java中就可以少些几行代码。
网上看了好多例子,大部分内容都是大同小异,(可能是因为各种转载的原因吧)。有很多细节都没有提及到。自己也是琢磨了半天才弄通。
自定义XML属性,就是可以方便的给自己定义的控件,添加自定义的属性,能快捷的给控件赋状态,赋属性。
关于自定义控件,请参考我的这篇博客:《探究Android中通过继承ViewGroup自定义控件的原理》http://www.cnblogs.com/kross/p/3378395.html
自定义控件需要如下几步:
1.自定义控件
2.自定义属性
3.在布局文件中使用自定义控件和自定义属性
自定义控件,就直接写一个现在主流的Tab控件,上面一个图片,下面一行Text组成的控件,通过继承LinearLayout来实现。代码如下:
/res/layout/tab.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview_tab_icon"
android:layout_width="match_parent"
android:layout_height="48dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/textview_tab_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="KKKKKKKK"/>
</LinearLayout>
/src/view/MyTab.java //注意这里的包名是view
package view; import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; import com.kross.customattr.R; public class MyTab extends LinearLayout { private static final String TAG = "MyTab"; private ImageView iv = null;
private TextView tv = null; public MyTab(Context context, AttributeSet attrs) {
super(context, attrs); LayoutInflater.from(context).inflate(R.layout.tab, this, true);
iv = (ImageView)this.findViewById(R.id.imageview_tab_icon);
tv = (TextView)this.findViewById(R.id.textview_tab_text); iv.setImageResource(R.drawable.home);
tv.setText("aaaaa"); }
}
OK,这样自定义控件就完成了,需要注意的是,看好这个MyTab的包名是view。
接下来,我们要自定义XML属性
在/res/values目录下建一个attr.xml文件。里面写如下代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTab">
<attr name="tab_icon" format="reference"/>
<attr name="tab_name" format="reference"/>
</declare-styleable>
</resources>
这里需要注意的是:
declare-styleable的name要和自定义的类名一样,(刚刚我们建了一个MyTab类,这里也必须是MyTab)
里面的attr name就是属性的名字了,跟layout_width一样,format有好几种不同的值:string , integer , dimension , reference , color , enum.
当format是enum,枚举类型的时候,里面的要写成这个样子:
<attr name="testEnum">
<enum name="fill_parent" value="-1"/>
<enum name="wrap_content" value="-2"/>
</attr>
自定义属性完成后,可以看看R.java文件的变化。
然后,我们需要给MyTab类中添加一些处理自定义属性的代码,代码更新为如下:
package view; import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; import com.kross.customattr.R; public class MyTab extends LinearLayout { private static final String TAG = "MyTab"; private ImageView iv = null;
private TextView tv = null; public MyTab(Context context, AttributeSet attrs) {
super(context, attrs); LayoutInflater.from(context).inflate(R.layout.tab, this, true);
iv = (ImageView)this.findViewById(R.id.imageview_tab_icon);
tv = (TextView)this.findViewById(R.id.textview_tab_text); iv.setImageResource(R.drawable.home);
tv.setText("aaaaa"); TypedArray attrArray = context.obtainStyledAttributes(attrs, R.styleable.MyTab);
int count = attrArray.getIndexCount();
for (int i = 0; i < count; i++) {
int attrName = attrArray.getIndex(i);
switch (attrName) {
case R.styleable.MyTab_tab_icon:
iv.setImageResource(attrArray.getResourceId(R.styleable.MyTab_tab_icon, R.drawable.ic_launcher));
break;
case R.styleable.MyTab_tab_name:
tv.setText(attrArray.getString(R.styleable.MyTab_tab_name));
break;
}
}
attrArray.recycle(); }
}
最后一步,我们就是在布局文件中使用它
看如下代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <view.MyTab
android:layout_width="match_parent"
android:layout_height="wrap_content"
tab_icon="@drawable/ic_launcher"
/>
</LinearLayout>
这样写是不起作用的,网上很多地方都说,要加上命名空间,注意LinearLayout里面新增的命名空间:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:kross="http://schemas.android.com/apk/res/view"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <view.MyTab
android:layout_width="match_parent"
android:layout_height="wrap_content"
kross:tab_icon="@drawable/ic_launcher"
/>
</LinearLayout>
给LinearLayout加上命名空间,并给tab_icon属性前也加上命名空间,但eclipse就报错了:error: No resource identifier found for attribute 'tab_icon' in package 'view'
在下研究了半天,终于发现问题所在,这个命名空间必须要和manifest文件中的package属性是一样的值,这也就意味着:自定义控件的类必须要放到项目自动创建的包里面,比如我这个例子,manifest文件中package的值是com.kross.customattr,MainActivity所在的位置就是com.kross.customattr,我们需要把MyTab类,放到com.kross.customattr中,然后吧activity_main.xml里面的命名空间改成如下就OK了:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:kross="http://schemas.android.com/apk/res/com.kross.customattr"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <com.kross.customattr.MyTab
android:layout_width="match_parent"
android:layout_height="wrap_content"
kross:tab_icon="@drawable/ic_launcher"
/>
</LinearLayout>
这样就可以运行了,但把自定义控件放到activity一起会觉得很蛋疼,貌似这是一个bug,请参考这里http://code.google.com/p/android/issues/detail?id=9656。
貌似2010年就提出了,但现在也没有修复。
转载请注明出处:http://www.cnblogs.com/kross/p/3458068.html
Android自定义XML属性以及遇到的命名空间的问题的更多相关文章
- Android自定义XML属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-style ...
- 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属性
在android开发过程中,用到系统的View时候可以通过XML来定义一些View的属性.比如ImageView: android:src 和android:scaleType为ImageVie ...
- android AndroidManifest.xml 属性详细解析
一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...
- android自定义view属性
第一种 /MainActivity/res/values/attrs.xml <?xml version="1.0" encoding="utf-8"?& ...
- 【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标 ...
随机推荐
- Git 命令及使用经验
手册中的基本命令: CONFIGURE TOOLING Configure user information for all local repositories $ git config --glo ...
- 20155211 2016-2017-2 《Java程序设计》第2周学习总结
20155211 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 通过对教材的阅读,我理解到Java中对于整数,浮点数等类型的定义与c语言基本相同. 对字面常 ...
- WPF 任务栏颜色 - 简书
原文:WPF 任务栏颜色 - 简书 先看看效果,这种效果可以用来做进度条或者消息通知闪烁. image.png image.png image.png image.png 有一个好消息 ...
- 北京Uber优步司机奖励政策(4月13日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- RHSCA模拟考试
开始考试:桌面是个黑框子 点击reboot按钮,破解密码 开机成功,输入startx进入图形界面 不能复制,要在物理机用ssh root@172.25.0.11 远程连接,就可以复制粘贴了 * Hos ...
- L016-linux系统文件权限体系实战深入讲解小节
L016-linux系统文件权限体系实战深入讲解小节 不知道今天能不能写完哈,能写完发出来就是这周发两次小结了,有进步哦,不过L015和L016两节课内容也确实不多,进入正题 上一课学到了chmod. ...
- C#反射的简单示例
反射(Reflection)可以在运行时获 得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等.还可以获得每个成员的名称.限定符和参数等反正说白了就 ...
- Python中的装饰器的使用及固定模式
装饰器的使用: 在不想修改函数的调用方式,但是想给函数添加内容的功能的时候使用 为什么使用装饰器: 软件实体应该是可扩展,而不可修改的.也就是说,对扩展是开放的,而对修改是封闭的. 因此,引出 ...
- 「日常训练」The Necklace(UVA-10054)
代码 for(int i=0; i!=n; ++i) { int u = cin.nextInt(); int v = cin.nextInt(); edges.add(new Edge(u,v)); ...
- 「Leetcode」976. Largest Perimeter Triangle(C++)
分析 好久不刷题真的思维僵化,要考虑到这样一个结论:如果递增的三个数\(x_i,x_{i+1},x_{i+2}\)不符合题意,那么最大的两边之差一定大于等于第一条边,那么任何比第一条边小的都不能成立. ...