- 浏览: 341390 次
- 性别:
- 来自: 安徽
文章分类
最新评论
-
secally:
楼主可以上传个例子吗,谢谢,我咋跑不起来,,eclipse不是 ...
Android自动打包、签名、优化、上传ANT脚本 -
secally:
请问下执行命令是啥,ant release 还是?麻烦指教谢谢 ...
Android自动打包、签名、优化、上传ANT脚本 -
trp1022:
好牛啊
程序员不能不通文史 -
sunny78117:
谢谢,写得好
Android bitmap优化 -
futurebp:
这是什么类型的脚本啊?
Android自动打包、签名、优化、上传ANT脚本
Android Tutorial #4.2: Passing custom attributes via XML resource files
- 博客分类:
- Android
In tutorial #4.1, I mentioned that we passed custom attributes for the text and image variables from the XML resource file to our custom class. This is a critical skill for performing true object-oriented programming and how to do it wasn’t obvious from Google’s Android API Demos.
Luckily I was pointed to the solution myself by an experienced Android programmer in Guatemala by the username of cadlg (thanks again!). If you want to see the official Google Android example though, look at Android’s APIDemos’ custom LabelView example.
So here we go. We’ll use the same code as Tutorial 4.1 to keep this simple.
Setting Up Your Custom Class’s XML Resource Files
We’ll only review the code for the TextOnlyButton as it’s identical in concept to the ImageOnlyButton.
First we’ll create a new file in /res/values called attrs.xml
<?xml version=”1.0″ encoding=”utf-8″?> <resources> <declare-styleable name=”TextOnlyButton”> <attr name=”textColorNotFocused” format=”integer”/> <attr name=”textColorFocused” format=”integer”/> </declare-styleable> </resources>
As you see, we first declared a ’styleable’ with the name of our custom Class. Two attributes were then added to contain the values of our focused & unfocused text colors. By default, attributes have values of String, but in our case, we needed integers to represent the resource id’s we’ll declare in our colors.xml file. You can also declare formats such as “boolean” & others if that suits the requirements of your own project.
Next, we declare values for these custom attributes in our layout’s XML file: tutorial4.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:pj=”http://schemas.android.com/apk/res/com.pocketjourney.tutorials” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:padding=”10px”> <com.pocketjourney.view.TextOnlyButton android:id=”@+id/text_only_button” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_marginTop=”5px” style=”?android:attr/buttonStyleSmall” android:text=”Text Button” pj:textColorNotFocused=”@drawable/white” pj:textColorFocused=”@drawable/android_orange” /> </LinearLayout>
Referring to our new attributes is actually a two step process. First we declared a new namespace (in our case called ‘pj’ as short for PocketJourney) in the parent layout of our custom class:
xmlns:pj=”http://schemas.android.com/apk/res/com.pocketjourney.tutorials”
Next we specified the values of our new attributes in the XML usage of our TextOnlyButton:
pj:textColorNotFocused=”@drawable/white”
pj:textColorFocused=”@drawable/android_orange”
Now you can see why we specified our format=”integer”. Our custom attributes point to the resource id’s of colors specified in our colors.xml file.
Retrieving Custom Attributes During Class Instantiation
Since our Activity has many constructors, we delegate the attribute parsing to an init() method to keep our code clean.
int notFocusedTextColor, f ocusedTextColor;
private void init(AttributeSet attrs) { Resources.StyledAttributes a = getContext().obtainStyledAttributes(attrs,R.styleable.TextOnlyButton ); notFocusedTextColor = a.getColor(R.styleable.TextOnlyButton_textColorNotFocused , 0xFF000000); focusedTextColor = a.getColor(R.styleable.TextOnlyButton_textColorFocused , 0xFF000000); }
By now you’ve undoubtedly seen the AttributeSet that is always passed into an Activity. Well now you get to use it. First we obtain the StyledAttributes instance by requesting just the StyledAttributes for our custom Class. Next, we call the getColor() and pass two variables: the name of the attribute we want along with a default value in case the user did not specify one.
Take note of our styled attribute’s name as it’s a combination of our custom class’s name and the attribute we specified in the attrs.xml file (e.g.TextOnlyButton_textColorNotFocused).
And That’s It
You can now readily pass your own custom attributes and keep your View variables cleanly enclosed in your XML files. You can download the source to see for yourself. Just look at Tutorial #4.
发表评论
-
抓取Android WebView截屏
2011-08-19 21:33 7669一般使用setDrawingCacheEnabled(true ... -
Android bitmap优化
2011-08-11 17:02 5990直接使用ImageView显示 ... -
Android模拟器更改分辨率后 无法取得分辨率问题
2011-05-11 21:06 2861在PC上更改模拟器分辨率为480*800 (如果你的PC界面 ... -
获取屏幕硬件分辨率方法 收藏
2011-05-11 16:28 1923TextView tv=(TextVie ... -
Bytecode for the Dalvik VM
2011-05-10 22:35 1370http://www.netmite.com/android/ ... -
Android DEX反编译后部分代码解析
2011-05-10 22:07 3947一个private 方法 private Stri ... -
反编译并且修改Android APK包
2011-05-10 16:57 3138為了某個實驗的動機,我們評估反編譯 Android ... -
Android Ant编译时候进行混淆
2010-10-18 13:49 3835版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
自定义Ant Task
2010-09-13 17:58 2999版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
Android自动打包、签名、优化、上传ANT脚本
2010-07-26 16:51 13994版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
Android自定义含有CheckBox的Adapter遇到的问题
2010-06-23 13:48 4129版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
QR code类库
2010-06-03 23:11 2336Google code发现的好东西。 QR code 类库 ... -
Android QQ,一个WEBOS APP诞生!!(两个QQ对比!)
2010-04-10 12:05 1880请看这个大哥的言论 http://linuxtoy.or ... -
Android 、BlackBerry 文本对齐方式对比
2010-02-22 13:11 1815版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
教你如何开关Android的APN网络
2010-01-08 11:36 6060版权所有,转载请注明来自Mobile Developer (h ... -
史上最强NDK入门项目实战
2009-10-10 13:29 2691以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
使用TimerTask控制ProgressBar
2009-10-10 13:21 1127以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
自定义List布局
2009-10-10 13:19 1935以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
重画Progressbar的进度
2009-10-10 13:15 1944以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
谈谈Android中文短信的实现
2009-10-10 13:13 2183似 乎Google在推出G1时 ...
相关推荐
kset:驱动的骨架kobject的容器,体现设备驱动的层次关系kset_create_and_add()函数kset_create()函数/*注册消息发送接口
Resource Sets and Configurations Material Design Basics Dealing with Threads Requesting Permissions Assets, Files, and Data Parsing Tutorial #11 - Adding Simple Content Tutorial #12 - Displaying the ...
### XML教程:理解XML及其对网络的影响 #### XML:一种扩展标记语言 XML,全称为“可扩展标记语言”(Extensible Markup Language),是万维网联盟(W3C)制定的一项标准,允许用户创建自定义标签。XML的出现简化...
在IT领域,XML(可扩展标记语言)与Java的结合为数据处理和应用程序间的数据交换提供了强大而灵活的解决方案。本文将深入探讨如何利用Java处理XML文档,通过使用Document Object Model (DOM),Simple API for XML ...
#### Tutorial #1: 创建与列表化Visualforce Pages **步骤1:启用Visualforce开发模式** - 首先确保您的Salesforce环境已启用Visualforce开发模式。 - 这一步骤通常在设置中完成,确保您可以访问所有Visualforce...
Android, the next-generation open mobile platform from Google and the Open Handset Alliance, is poised to become a significant player in the mobile device market. The Android platform gives developers...
Tutorial #1 Getting started.mp4
Tutorial 1: Setting up DirectX 11 with Visual Studio Tutorial 2: Creating a Framework and Window Tutorial 3: Initializing DirectX 11 Tutorial 4: Buffers, Shaders, and HLSL Tutorial 5: ...
Tutorial #3 Geometry and texture retouching.mp4
### 标题:FLUENT TUTORIAL GUIDE #### 描述:FLUENT TUTORIAL GUIDE #### 标签:FLUENT #### 部分内容:ANSYS FLUENT Tutorial Guide ### 知识点分析: #### 1. **ANSYS FLUENT简介** ANSYS FLUENT是一款...
根据给定的文件信息,以下是从“Target for ArcGIS Tutorial”中提炼出的关键知识点: ### 标题:Target for ArcGIS教程 此标题表明文档旨在作为用户学习如何使用Target for ArcGIS软件的指南。Target for ArcGIS...
Part reference, part tutorial, this book thoroughly explains the shift from fixed-functionality graphics hardware to the new era of programmable graphics hardware and the additions to the OpenGL API ...
Tutorial #2 Georeferencing, tiling and region-of-interest.mp4
##### 4.2 使用JasperReports创建最终报表 接下来,使用JasperReports的API来填充`.jrxml`文件中的数据。JasperReports支持多种数据源,如数据库查询结果、XML文件等。填充完成后,可以通过JasperReports生成各种...
- **描述**:“XML Tutorial,XML format syntax, easy to understand.” 描述了文档内容涉及XML的基本格式和语法,并强调了易于理解的特点。 #### 关键知识点详述 1. **XML文档结构** - **XML声明**: 文档的第一...
#4.2_RNN_循环神经网络_分类_(PyTorch_tutorial_神经网络_教学)
《深入探索XMLSpy 2012:Altova XMLSpy的全面教程》 XMLSpy是Altova公司开发的一款强大的XML编辑器和开发工具,广泛应用于XML文档的创建、编辑、转换以及验证等环节。本教程将围绕XMLSpy 2012版本,详细解析其功能...
### FRAGSTATS4.2中文教程知识点概览 #### 一、FRAGSTATS简介与准备工作 **FRAGSTATS4.2**是一款专为生态学家和地理信息系统(GIS)用户设计的强大软件工具,主要用于量化和分析景观的破碎化程度。通过一系列的...
org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter <type-mapping>Oracle9i ``` #### 步骤4:复制Oracle JDBC驱动 将Oracle JDBC驱动复制到JBOSS_HOME/server/default/lib目录下。 #### ...