- 浏览: 1504529 次
- 性别:
- 来自: 南京
文章分类
- 全部博客 (419)
- XMPP (19)
- Android (180)
- Java (59)
- Network (4)
- HTML5 (13)
- Eclipse (9)
- SCM (23)
- C/C++ (4)
- UML (4)
- Libjingle (15)
- Tools&Softwares (29)
- Linphone (5)
- Linux&UNIX (6)
- Windows (18)
- Google (10)
- MISC (3)
- SIP (6)
- SQLite (5)
- Security (4)
- Opensource (29)
- Online (2)
- 文章 (3)
- MemoryLeak (10)
- Decompile (5)
- Ruby (1)
- Image (1)
- Bat (4)
- TTS&ASR (28)
- Multimedia (1)
- iOS (20)
- Asciiflow - ASCII Flow Diagram Tool.htm (1)
- Networking (1)
- DLNA&UPnP (2)
- Chrome (2)
- CI (1)
- SmartHome (0)
- CloudComputing (1)
- NodeJS (3)
- MachineLearning (2)
最新评论
-
bzhao:
点赞123!
Windows的adb shell中使用vi不乱码方法及AdbPutty -
wahahachuang8:
我觉得这种东西自己开发太麻烦了,就别自己捣鼓了,找个第三方,方 ...
HTML5 WebSocket 技术介绍 -
obehavior:
view.setOnTouchListenerview是什么
[转]android 一直在最前面的浮动窗口效果 -
wutenghua:
[转]android 一直在最前面的浮动窗口效果 -
zee3.lin:
Sorry~~
When I build "call ...
Step by Step about How to Build libjingle 0.4
http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml
The Android Developer Guide has a section called Building Custom Components . Unfortunately, the discussion of XML attributes only covers declaring the control inside the layout file and not actually handling the values inside the class initialisation. The steps are as follows:
1. Declare attributes in values\attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyCustomView"> <attr name="android:text"/> <attr name="android:textColor"/> <attr name="extraInformation" format="string" /> </declare-styleable> </resources>
Notice the use of an unqualified name in the declare-styleable
tag. Non-standard android attributes like extraInformation
need to have their type declared. Tags declared in the superclass will
be available in subclasses without having to be redeclared.
2. Create constructors
Since there are two constructors that use an AttributeSet
for initialisation, it is convenient to create a separate initialisation method for the constructors to call.
private void init(AttributeSet attrs) { TypedArray a=getContext().obtainStyledAttributes( attrs, R.styleable.MyCustomView); //Use a Log.i("test",a.getString( R.styleable.MyCustomView_android_text)); Log.i("test",""+a.getColor( R.styleable.MyCustomView_android_textColor, Color.BLACK)); Log.i("test",a.getString( R.styleable.MyCustomView_android_extraInformation)); //Don't forget this a.recycle(); }
R.styleable.MyCustomView
is an autogenerated int[]
resource where each element is the ID of an attribute. Attributes are
generated for each property in the XML by appending the attribute name
to the element name. Attributes can then be retrieved from the TypedArray
using various get
functions. If the attribute is not defined in the defined in the XML, then null
is returned. Except, of course, if the return type is a primitive, in which case the second argument is returned.
If you don't want to retrieve all of the attributes, it is possible
to create this array manually.The ID for standard android attributes are
included in android.R.attr
, while attributes for this project are in R.attr
.
int attrsWanted[]=new int[]{android.R.attr.text, R.attr.textColor};
Please note that you should not
use anything in android.R.styleable
, as per this thread
it may change in the future. It is still in the documentation as being to view all these constants in the one place is useful.
3. Use it in a layout files such as layout\main.xml
Include the namespace declaration xmlns:app="http://schemas.android.com/apk/res/com.mycompany.projectname"
in the top level xml element.
<com.mycompany.projectname.MyCustomView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:text="Test text" android:textColor="#FFFFFF" app:extraInformation="My extra information"; />
Reference the custom view using the fully qualified name.
Android LabelView Sample
If you want a complete example, look at the android label view sample.
TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.LabelView); CharSequences=a.getString(R.styleable.LabelView_text);
<declare-styleable name="LabelView"> <attr name="text"format="string"/> <attr name="textColor"format="color"/> <attr name="textSize"format="dimension"/> </declare-styleable>
<com.example.android.apis.view.LabelView android:background="@drawable/blue" android:layout_width="fill_parent" android:layout_height="wrap_content" app:text="Blue"app:textSize="20dp"/>
This is contained in a LinearLayout
with a namespace attribute:
xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis"
Links
- Pocket Journey Tutorial
- StackOverflow Thread: Retrieving an XML attribute for custom control
- How do I use obtainStyledAttributes with internal themes of Android
- Defining custom attributes + list of supported attribute formats
发表评论
-
[Android] 为Android安装BusyBox —— 完整的bash shell
2013-12-27 10:19 1490http://www.cnblogs.com/xiaowen ... -
Windows的adb shell中使用vi不乱码方法及AdbPutty
2013-12-27 10:17 7576http://www.veryhuo.com/down/ht ... -
AppMobi推出新XDK,可创建测试PhoneGap项目
2012-09-03 13:39 2637AppMobi今天发布了一个新的工具PhoneGap Mobi ... -
Sencha
2012-09-03 12:59 1185http://www.sencha.com/ Se ... -
jQuery Mobile学习
2012-09-01 12:33 1691使用Jquery Mobile设计Android通讯录 ... -
BackBone
2012-09-01 12:34 1260Backbone.js 是一种重量级javascript M ... -
jQTouch
2012-08-30 15:57 984A Zepto/jQuery plugin for mobil ... -
SwiFTP
2012-08-30 15:43 1307SwiFTP is a FTP server that run ... -
kWS
2012-08-30 15:41 1198kWS is a lightweight and fast W ... -
jQuery Mobile
2012-08-30 15:07 1027http://jquerymobile.com/ -
PhoneGap
2012-08-30 15:07 1044http://phonegap.com/ -
Android Button background image pressed/highlighted and disabled states without
2012-08-06 12:49 1678http://shikii.net/blog/android- ... -
[AndriodTips]Image, saved to sdcard, doesn't appear in Android's Gallery app
2012-08-04 16:15 1158http://stackoverflow.com/questi ... -
Voice detection for Android
2012-07-23 11:39 2347Here it is, my fist JAVA applic ... -
[AndroidTip]local reference table overflow (max=512)的错误解决
2012-07-22 22:56 6049JNI层coding经常会遇到ReferenceTable o ... -
[AndroidTip]EditText如何初始状态不获得焦点?
2012-07-22 15:35 1226最简单的办法是在EditText前面放置一个看不到的Linea ... -
[AndroidTip]android textview滚动条
2012-07-21 14:29 1298本来是想做一个显示文字信息的,当文字很多时View的高度不能超 ... -
Google公布Android 4.1完整功能
2012-07-16 09:48 3185http://www.android.com/about/je ... -
Android开发:使用AudioTrack播放PCM音频数据【附源码】
2012-07-13 15:20 20867http://www.linuxidc.com/Linux/2 ... -
Android上的行车记录仪
2012-07-11 22:31 2010MyCar Recorder DailyRoads
相关推荐
Product discovery using a mobile device 172 Mobile payments 173 Configurations 173 PCI Standard 175 Point of Sale 176 Proximity technologies 178 Social networking 178 Table of Contents [vi ] ...
Using a Bluetooth Headset 344 Managing Network and Wi-Fi Connections 345 Monitoring and Managing Your Internet Connectivity 345 Managing Active Connections 346 Managing Your Wi-Fi 347 ...
7.1.3. Declaring DispatcherServlet in web.xml 7.2. Processing multipart form data 7.2.1. Configuring a multipart resolver 7.2.2. Handling multipart requests 7.3. Handling exceptions 7.3.1. Mapping ...
This web archive file is actually an XML file with the contents of the page encoded in Base64 format, and also contains all sub-frames and objects embedded into a page, such as images etc. Please ...
Programming Windows Presentation Foundation By Ian Griffiths, Chris Sells ... an XML-based markup language (XAML) for declaring the structure of your Windows UI; and a radical new model for controls
Using Ant to Build a Web Application Section A.7. Example: Building a Web Application Section A.8. Using Ant to Create a WAR File Section A.9. Example: Creating a Web Application WAR File Index
Using Ant to Build a Web Application Section A.7. Example: Building a Web Application Section A.8. Using Ant to Create a WAR File Section A.9. Example: Creating a Web Application WAR File Index
3. The third program finds the maximum element in a 2D array and prints its position and value. 4. The fourth program concatenates two strings using the `strcat` function. 5. The fifth program ...
The content assist features are driven by metadata about a dialect, currently done using XML files, conforming to a schema that lives at http://www.thymeleaf.org/xsd/thymeleaf-extras-dialect-2.1.xsd. ...
Calculate a factorial by using while loop In this example we are going to find out the factorial of 12 by using the while loop. In while loop the loop will run until the condition we have given gets ...
source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven ...
14.5.1. Declaring Filter and Validator Rules 14.5.2. Creating the Filter and Validator Processor 14.5.3. Retrieving Validated Fields and other Reports 14.5.3.1. Querying if the input is valid 14.5...
如果远程Bean A有个方法A.blah(B),那么你需要为A建立一个created,为B建立一个converted. 配置文件init部分声明那些用于建立远程bean和在方法调用中转换bean的类.这部分是可选择性配置的,多数情况下可以不必使用它,...
Using a database pool in an application 70 Accessing a server-scoped database pool 70 Accessing an application-scoped database pool from the same application 74 Accessing an application-scoped ...
In order to use this function, all you have to do is provide a valid HINTERNET handle obtained using a standard InternetOpen() call. If you want, you can provide a handle to a progress window (and an...
Based on the provided information from "iOS 5 Programming Cookbook" by Vandad Nahavandipoor, we can derive a comprehensive set of knowledge points related to iOS development using Objective-C....