- 浏览: 335350 次
- 性别:
- 来自: 安徽
文章分类
最新评论
-
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 7602一般使用setDrawingCacheEnabled(true ... -
Android bitmap优化
2011-08-11 17:02 5893直接使用ImageView显示 ... -
Android模拟器更改分辨率后 无法取得分辨率问题
2011-05-11 21:06 2803在PC上更改模拟器分辨率为480*800 (如果你的PC界面 ... -
获取屏幕硬件分辨率方法 收藏
2011-05-11 16:28 1884TextView tv=(TextVie ... -
Bytecode for the Dalvik VM
2011-05-10 22:35 1321http://www.netmite.com/android/ ... -
Android DEX反编译后部分代码解析
2011-05-10 22:07 3873一个private 方法 private Stri ... -
反编译并且修改Android APK包
2011-05-10 16:57 3064為了某個實驗的動機,我們評估反編譯 Android ... -
Android Ant编译时候进行混淆
2010-10-18 13:49 3808版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
自定义Ant Task
2010-09-13 17:58 2964版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
Android自动打包、签名、优化、上传ANT脚本
2010-07-26 16:51 13936版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
Android自定义含有CheckBox的Adapter遇到的问题
2010-06-23 13:48 4089版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
QR code类库
2010-06-03 23:11 2292Google code发现的好东西。 QR code 类库 ... -
Android QQ,一个WEBOS APP诞生!!(两个QQ对比!)
2010-04-10 12:05 1849请看这个大哥的言论 http://linuxtoy.or ... -
Android 、BlackBerry 文本对齐方式对比
2010-02-22 13:11 1780版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
教你如何开关Android的APN网络
2010-01-08 11:36 5973版权所有,转载请注明来自Mobile Developer (h ... -
史上最强NDK入门项目实战
2009-10-10 13:29 2665以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
使用TimerTask控制ProgressBar
2009-10-10 13:21 1048以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
自定义List布局
2009-10-10 13:19 1858以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
重画Progressbar的进度
2009-10-10 13:15 1914以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
谈谈Android中文短信的实现
2009-10-10 13:13 2144似 乎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 ...
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
Building Parts Templates GoJS Beginner Tutorial #4.rar
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
Building Parts Parts and Panels GoJS Beginner Tutorial #2.rar
Exploring a GoJS Application in Real-time GoJS Beginner Tutorial #6.rar
#4.2_RNN_循环神经网络_分类_(PyTorch_tutorial_神经网络_教学)
#4.2_怎么样从_Windows_通过_SSH_远程_Linux_(Linux_机器学习_教程教学_tutorial)
#4.2_NEAT_监督学习_Supervised_learning_(机器学习_进化算法_Evolutionary_Algor
模版方法模式模版方法模式其实就是我们经常用到的继承先创建父类模版,子类继承父类模版,在需要的接口中做子类具体的实现即可例子:this.begin = funct
迭代器模式迭代器模式是指提供一种方法顺序访问一个聚合对象中的各个元素,并且又不用关系聚合对象的内部结构我们常用的数组的迭代方法即是迭代器例如:// 这里在不需要
android studio版本: android stuodio 1.5.1 ...gstreamer在android的例子:Android tutorial 3: Video, 链接:http://docs.gstreamer.com/display/GstSDK/Android+tutorial+3%3A+Video 在android studiao下编译通过
Duration Assertion: 持续时间断言,主要用于响应时间的判断Apply to: 判断的范围选择,一般选择Main sample onlyMain
空间拆分命名空间可以拆分,当命名空间代码量很大时,拆分可以提高可维护性:// 拆分成第一块// 拆分成第二块但要注意,虽然 ns 被拆分成两个代码块了,但这两个