Android中如何自定义xml属性-创新互联

这篇“Android中如何自定义xml属性”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Android中如何自定义xml属性”文章吧。

公司主营业务:网站设计制作、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出龙山免费做网站回馈大家。

1. 首先创建一个新的android application.

2. 创建属性
在res/values/ 下创建一个attr.xml 文件,定义好需要的attributes

<?xml version="1.0" encoding="utf-8"?>   <resources>       <declare-styleable name="custom">           <attr name="text" format="string" />           <attr name="size" format="integer"/>           <attr name="color" format="reference|color"/>       </declare-styleable>   </resources>

3. 创建自定义的View

创建一个View, CustomView 继承自View(根据具体的情况,如果需求和已经存在的widget或者layout相差不大,就继承,重写一些方法)

package com.hualu.androidview;   import android.content.Context;   import android.content.res.TypedArray;   import android.graphics.Canvas;   import android.graphics.Paint;   import android.util.AttributeSet;   import android.view.View;   public class CustomView extends View {       private  Paint p = null;       private String text =  null;       public CustomView(Context context) {           super(context);           initCustomView() ;       }       public CustomView(Context context, AttributeSet attrs){           super(context, attrs ) ;           initCustomView() ;           TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.custom) ;           int indexCount = a.getIndexCount() ;           for(int i = 0 ; i < indexCount ; i ++){               int index = a.getIndex(i) ;               switch (index) {               case R.styleable.custom_text:                   text = a.getString(index) ;                   break;               case R.styleable.custom_size:                   p.setTextSize(a.getInt(index, 0));                    break;               case R.styleable.custom_color:                   p.setColor(a.getColor(index, 0xFF000000)) ;                   break;               }           }           a.recycle() ;       }      void initCustomView(){            p = new Paint();            p.setAntiAlias(true);       } ;       @Override       protected void onDraw(Canvas canvas) {           super.onDraw(canvas);           canvas.drawText(text, 10, 10, p) ;       }   }

4. 在layout的文件使用自定义的view

<RelativeLayout xmlns: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" >       <TextView           android: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.CustomView           android:layout_width="wrap_content"           android:layout_height="wrap_content"           custom:text="custom view"           custom:color="#00FF00"           custom:size="18"           />   </RelativeLayout>

以上就是关于“Android中如何自定义xml属性”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注创新互联行业资讯频道。

新闻名称:Android中如何自定义xml属性-创新互联
路径分享:https://www.cdcxhl.com/article26/diisjg.html

成都网站建设公司_创新互联,为您提供网站内链网站改版做网站域名注册商城网站App开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

小程序开发