Android中如何自定义xml属性
Android中如何自定义xml属性
这篇“Android中如何自定义xml属性”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Android中如何自定义xml属性”文章吧。
1. 首先创建一个新的android application.
2. 创建属性
在res/values/ 下创建一个attr.xml 文件,定义好需要的attributes
<?xmlversion="1.0"encoding="utf-8"?><resources><declare-styleablename="custom"><attrname="text"format="string"/><attrname="size"format="integer"/><attrname="color"format="reference|color"/></declare-styleable></resources>
3. 创建自定义的View
创建一个View, CustomView 继承自View(根据具体的情况,如果需求和已经存在的widget或者layout相差不大,就继承,重写一些方法)
packagecom.hualu.androidview;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.util.AttributeSet;importandroid.view.View;publicclassCustomViewextendsView{privatePaintp=null;privateStringtext=null;publicCustomView(Contextcontext){super(context);initCustomView();}publicCustomView(Contextcontext,AttributeSetattrs){super(context,attrs);initCustomView();TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.custom);intindexCount=a.getIndexCount();for(inti=0;i<indexCount;i++){intindex=a.getIndex(i);switch(index){caseR.styleable.custom_text:text=a.getString(index);break;caseR.styleable.custom_size:p.setTextSize(a.getInt(index,0));break;caseR.styleable.custom_color:p.setColor(a.getColor(index,0xFF000000));break;}}a.recycle();}voidinitCustomView(){p=newPaint();p.setAntiAlias(true);};@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);canvas.drawText(text,10,10,p);}}
4. 在layout的文件使用自定义的view
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:custom="http://schemas.android.com/apk/res/com.hualu.androidview"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:text="@string/hello_world"/><com.hualu.androidview.CustomViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"custom:text="customview"custom:color="#00FF00"custom:size="18"/></RelativeLayout>
以上就是关于“Android中如何自定义xml属性”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注恰卡编程网行业资讯频道。
推荐阅读
-
我的世界windows版怎么下载手机版
-
android(如何快速开发框架 小米note开发版MIUI,安卓6.0,怎么安装Xposed框架)
稳定版,你必须先根除。你上网搜索安卓可以叫别人s框架,对方可以把框架做成jar包,把这个jar包加载到项目目录的libs文件中使...
-
android(studio 虚拟机启动不了 android studio可以当模拟器用吗)
androidstudio可以当模拟器用吗?AmdCUP引导模拟器有点复杂。雷电模拟器上的抖音怎么登录不上?不是,闪电模拟调用...
-
从实践中学习手机抓包与数据分析(android 手机抓包app)
android手机抓包app?netcapture抓包精灵app(手机抓包工具)又名sslcapture,是什么专业的安卓手机抓...
-
android(studio全局搜索 android studio怎么看app界面)
androidstudio怎么看app界面?在设备桌面点击运用直接进入到App界面,就也可以参与其他你的操作了。android-...
-
怎么把android框架源代码拉到本地(android studio如何运行别人的源代码)
androidstudio如何运行别人的源代码?androidstudio点击刚建在列表中你选择导入module,导入即可在用...
-
android(studio2022年使用教程 怎么安装Android studio详细教程)
怎么安装Androidstudio详细教程?androidstudio中haxm直接安装的方法追加:1、简单的方法打开Andr...
-
怎么使用Android基准配置文件Baseline Profile方案提升启动速度
怎么使用Android基准配置文件Baseline Profile方案提升启动速度...
-
HTML5如何实现禁止android视频另存为
HTML5如何实现禁止android视频另存为今天小编给大家分享一...
-
「原创」PHP实战-XML详细教程