再也不信网上流传的方法了!!android创建组件的真正写法!
2014-12-26 00:00
369 查看
摘要: android创建组件的真正写法
http://blog.csdn.net/jincf2011/article/details/6344678 气死我了 这货博客坑死大爷了大爷弄了两天各种毛病
一看官网原来这玩意儿不是这么写的
官网教程:
All of the view classes defined in the Android framework extend
To allow the Android Developer Tools to interact with your view, at a minimum you must provide a constructor that takes a
Define custom attributes for your view in a
Specify values for the attributes in your XML layout
Retrieve attribute values at runtime
Apply the retrieved attribute values to your view
This section discusses how to define custom attributes and specify their values. The next section deals with retrieving and applying the values at runtime.
To define custom attributes, add
This code declares two custom attributes,
Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to the
In order to avoid having to repeat the long namespace URI, the sample uses an
Notice the name of the XML tag that adds the custom view to the layout. It is the fully qualified name of the custom view class. If your view class is an inner class, you must further qualify it with the name of the view's outer class. further. For instance, the
Resource references within attribute values are not resolved
Styles are not applied
Instead, pass the
The Android resource compiler does a lot of work for you to make calling
Note that
太长了是吧 简单说一下:
自定义组件NewView继承一个View类
重写构造函数
为了专业一点 让xml文件能配置这个组件 ,需要在values下的attrs.xml中定义一下自定义组件的参数
reference 代表引用资源文件 string代表字符 color 代表颜色 dimension代表大小
好了现在可以在布局文件中引入这个组件了
app代表属性的namespace,如果用android studio的话
根元素加上
xmlns:app="http://schemas.android.com/apk/res-auto"
如果是eclipse 需要替换成
然后我们在构造函数中处理这些传入的参数
取到的参数怎么处理 就不用说了哈
http://blog.csdn.net/jincf2011/article/details/6344678 气死我了 这货博客坑死大爷了大爷弄了两天各种毛病
一看官网原来这玩意儿不是这么写的
官网教程:
All of the view classes defined in the Android framework extend
View. Your custom view can also extend
Viewdirectly, or you can save time by extending one of the existing view subclasses, such as
Button.
To allow the Android Developer Tools to interact with your view, at a minimum you must provide a constructor that takes a
Contextand an
AttributeSetobject as parameters. This constructor allows the layout editor to create and edit an instance of your view.
class PieChart extends View { public PieChart(Context context, AttributeSet attrs) { super(context, attrs); } }
Define Custom Attributes
To add a built-inViewto your user interface, you specify it in an XML element and control its appearance and behavior with element attributes. Well-written custom views can also be added and styled via XML. To enable this behavior in your custom view, you must:
Define custom attributes for your view in a
<declare-styleable>resource element
Specify values for the attributes in your XML layout
Retrieve attribute values at runtime
Apply the retrieved attribute values to your view
This section discusses how to define custom attributes and specify their values. The next section deals with retrieving and applying the values at runtime.
To define custom attributes, add
<declare-styleable>resources to your project. It's customary to put these resources into a
res/values/attrs.xmlfile. Here's an example of an
attrs.xmlfile:
<resources> <declare-styleable name="PieChart"> <attr name="showText" format="boolean" /> <attr name="labelPosition" format="enum"> <enum name="left" value="0"/> <enum name="right" value="1"/> </attr> </declare-styleable> </resources>
This code declares two custom attributes,
showTextand
labelPosition, that belong to a styleable entity named
PieChart. The name of the styleable entity is, by convention, the same name as the name of the class that defines the custom view. Although it's not strictly necessary to follow this convention, many popular code editors depend on this naming convention to provide statement completion.
Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to the
http://schemas.android.com/apk/res/androidnamespace, they belong to
http://schemas.android.com/apk/res/[your package name]. For example, here's how to use the attributes defined for
PieChart:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews"> <com.example.customviews.charting.PieChart custom:showText="true" custom:labelPosition="left" /> </LinearLayout>
In order to avoid having to repeat the long namespace URI, the sample uses an
xmlnsdirective. This directive assigns the alias
customto the namespace
http://schemas.android.com/apk/res/com.example.customviews. You can choose any alias you want for your namespace.
Notice the name of the XML tag that adds the custom view to the layout. It is the fully qualified name of the custom view class. If your view class is an inner class, you must further qualify it with the name of the view's outer class. further. For instance, the
PieChartclass has an inner class called
PieView. To use the custom attributes from this class, you would use the tag
com.example.customviews.charting.PieChart$PieView.
Apply Custom Attributes
When a view is created from an XML layout, all of the attributes in the XML tag are read from the resource bundle and passed into the view's constructor as anAttributeSet. Although it's possible to read values from the
AttributeSetdirectly, doing so has some disadvantages:
Resource references within attribute values are not resolved
Styles are not applied
Instead, pass the
AttributeSetto
, int, int)]obtainStyledAttributes(). This method passes back a
TypedArrayarray of values that have already been dereferenced and styled.
The Android resource compiler does a lot of work for you to make calling
, int, int)]obtainStyledAttributes()easier. For each
<declare-styleable>resource in the res directory, the generated R.java defines both an array of attribute ids and a set of constants that define the index for each attribute in the array. You use the predefined constants to read the attributes from the
TypedArray. Here's how the
PieChartclass reads its attributes:
public PieChart(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.getTheme().obtainStyledAttributes( attrs, R.styleable.PieChart, 0, 0); try { mShowText = a.getBoolean(R.styleable.PieChart_showText, false); mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0); } finally { a.recycle(); } }
Note that
TypedArrayobjects are a shared resource and must be recycled after use.
太长了是吧 简单说一下:
自定义组件NewView继承一个View类
重写构造函数
public NewView(Context context) { this(context,null); } public NewView(Context context, AttributeSet attrs) { super(context, attrs); }
为了专业一点 让xml文件能配置这个组件 ,需要在values下的attrs.xml中定义一下自定义组件的参数
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="NewView"> <attr name="pressedIcon" format="reference" /> <attr name="buttonIcon" format="reference"/> <attr name="text" format="string"/> <attr name="iconSize" format="dimension"/> <attr name="textSize" format="dimension"/> </declare-styleable> </resources>
reference 代表引用资源文件 string代表字符 color 代表颜色 dimension代表大小
好了现在可以在布局文件中引入这个组件了
<com.xxx.app.view.NewView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" app:text="@string/main_one" app:iconSize="20dp"/>
app代表属性的namespace,如果用android studio的话
根元素加上
xmlns:app="http://schemas.android.com/apk/res-auto"
如果是eclipse 需要替换成
xmlns:app="http://schemas.android.com/apk/res/包名"
然后我们在构造函数中处理这些传入的参数
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.NewView, 0, 0); try{ CharSequence text = a.getText(R.styleable.NewView_text); }finally{ a.recycle(); }
取到的参数怎么处理 就不用说了哈
相关文章推荐
- 针对网上流传的 Android 再按一次后退键退出应用程序方法改正
- 针对网上流传的 Android 再按一次后退键退出应用程序方法改正
- 详解Android App中创建ViewPager组件的方法
- 针对网上流传的"Android 再按一次后退键退出应用程序"方法改正
- 针对网上流传的"Android 再按一次后退键退出应用程序"方法改正
- Android高手进阶教程(二十一)之---Android中创建与几种解析xml的方法!
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- android中満屏,真正的全屏实现方法
- 解决asp.net ajax sys未定义,非网上流传方法
- 分享:网上流传的几种破解mysql root密码方法
- 【转】 Android用户界面——创建自定义组件
- 说说Android 两种为自定义组件添加属性的使用方法和区别 推荐
- Android使用原生组件WebView加载网页和数据的方法
- android插件(ECLIPSE) 安装android SDK2.0及创建2.0模拟器的方法
- Android中进入下一个Activity时在上一个Activity主动调用onDestroy()方法不会真正将上一个Activity从栈中删除
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- C#中IP地址与数字之间的互转(网上流传的方法有Bug)
- E-Form++可视化组件库图形元件的创建与使用方法探讨?
- Android开发中使用Eclipse创建工程后提示The project cannot be built until build path errors are resolved问题的解决方法
- android 通过代码创建页面组件