付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android中怎么在xml中设置自定义属性
Android中怎么在xml中设置自定义属性,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面在下将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。1.在项目中的values文件中创建attrs文件<?xml
version="1.0"
encoding="utf-8"?>
<resources>
<declare-styleable
name="QLoadingIndicatorView">
<attr
name="indicatorId"
format="integer"/>
<attr
name="indicatorColor"
format="color"/>
<attr
name="indicatorText"
format="string"/>
</declare-styleable>
</resources><pre
name="code"
class="java"></pre>
<h4><a
name="t3"></a>2.
在view中关联这些属性</h4>
<div><pre
name="code"
class="java">public
class
MyView
extends
LinearLayout
{
private
int
mIndicatorColor,mIndicatorId;
private
String
mIndicatorText;
public
MyView(Context
context,
AttributeSet
attrs,
int
defStyleAttr)
{
super(context,
attrs,
defStyleAttr);
//从xml的属性中获取到值,然后想怎么用就怎么用
TypedArray
ta=context.obtainStyledAttributes(attrs,R.styleable.QLoadingIndicatorView);
mIndicatorColor=ta.getColor(R.styleable.QLoadingIndicatorView_indicatorColor,Color.BLACK);//第二个参数是设置的默认值,当你不设置这个属性时会使用这个值
mIndicatorId=ta.getInt(R.styleable.QLoadingIndicatorView_indicatorId,1);
mIndicatorText=ta.getString(R.styleable.QLoadingIndicatorView_indicatorText,"abc");
ta.recycle();
}
}</pre><br>
<br>
</div>
<pre
name="code"
class="java"></pre><pre
name="code"
class="java"></pre>
<h4><a
name="t4"></a>3.xml文件中设置属性和命名空间</h4>
<div><pre
name="code"
class="java"><GridLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="/apk/res-auto"
//这就是自己定义的属性的命名空间,androidstudio是这样写的,添加属性的时候自动生成
android:rowCount="9"
android:columnCount="4">
<gressbaranimation.QLoadingIndicatorView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
app:indicatorId="0"
//自己的设置的属性
app:indicatorColor="#F00"
>
</gressbaranimation.QLoadingIndicatorView>
</GridLayout></pre><br>
<br
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 建立数字化审计工作制度
- 过敏原特异性IgE检测结果临床解读专家共识总结2026
- 2026年高职(管理会计)管理会计综合测试试题及答案
- 2026年市政一建建筑考试试题及答案
- 2026年全球经济增长趋势考试及答案
- 2026年护理社区招聘考试试题及答案
- 2024新版2026春八年级历史下册教学课件:第15课 开始全面建设小康社会
- 武汉“文化城”阅马场大气污染溯源与精准防治策略探究
- 人工智能赋能的主-配-微多层级电网协同运行技术探讨
- 艺术欣赏与鉴赏:2026年全国中小学生美术教育试题
- 海南大学硕士研究生入学考试复试政治审查表
- 数据中心搬迁规划方案
- 2-半乳甘露聚糖产品介绍北京瓜尔润
- 酒店英语面试问题及回答
- 天津高考英语词汇3500
- 历史专业英语词汇
- 吴冬冬:长方体和正方体的认识PPT
- 水文学课件ppt版 课件第七章
- 房屋租赁缴费明细表Excel模板
- GB/T 2677.8-1994造纸原料酸不溶木素含量的测定
- GB/T 20703-2006船舶电气装置取暖和烹调电器
评论
0/150
提交评论