Android中怎么自定义xml属性

发布时间:2021-06-26 15:45:50 作者:Leah
来源:亿速云 阅读:331

本篇文章给大家分享的是有关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属性,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. Android中xml中shape的属性助记
  2. Android自定义属性

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

android

上一篇:Android中位置管理器如何使用

下一篇:Android中怎么批量插入数据

相关阅读

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

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