温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中如何自定义xml属性

发布时间:2022-04-15 16:46:44 来源:亿速云 阅读:428 作者:zzz 栏目:编程语言

这篇“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属性”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI