Iwfu-安卓自定义控件xml属性---TypedArray

2023-10-15 12:38

本文主要是介绍Iwfu-安卓自定义控件xml属性---TypedArray,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这是一篇关于安卓自定义控件xml属性的博客

初次入门,笔尖寒酸。

以一个自定义带文字的图片控件为例:
自定义MyImageView继承LinearLayout

public class MyImageView extends LinearLayout {public MyImageView(Context context) {super(context);}public MyImageView(Context context, AttributeSet attrs) {super(context, attrs);}}

因为想实现带图片的文字(带文字的图片),初步想法是类似自定义ViewGroup然后添加入一个TextView和一个ImageView,只达到熟悉自定义组件提供xml属性即可。

在res文件夹新建资源文件attrs.xml,编写提供xml文件使用的属性:

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="MyImageView"><attr name="chanImageViewTitle" format="string|reference"></attr><attr name="chanImageViewSrc" format="reference"></attr><attr name="chanImageViewOrientation"><enum name="Vertical" value="0"></enum><enum name="Horizontal" value="1"></enum></attr></declare-styleable></resources>

其中,attr标签内name表示提供的属性名,format表示填写这个属性要输入的值的格式,可以用”|”隔开,允许多种输入值格式,自定义chanImageViewOrientation只有两个值,水平或垂直,用emu标签枚举出所有的类型即可。

然后修改MyImageView代码:

使用TypedArray读取xml属性值

/*** Created by Administrator on 2016/4/14.** 自定义带图片的TextView*/
public class MyImageView extends LinearLayout {public MyImageView(Context context) {super(context);}public MyImageView(Context context, AttributeSet attrs) {super(context, attrs);ImageView imageView = new ImageView(context);TextView textView = new TextView(context);//资源id,读取自定义属性的每一种属性时用这个值接收下int resourceId;//这里要填两个参数的方法,attrs如果不传入则控件无效//获取属性数组
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyImageView);//遍历属性数组,根据属性不同,进行不同操作int n = typedArray.getIndexCount();for (int i = 0; i < n; i++) {int attr = typedArray.getIndex(i);switch (attr) {// 处理文字case R.styleable.MyImageView_chanImageViewTitle :resourceId = typedArray.getResourceId(attr, 0);Log.d ("title","---"+resourceId+"---");if (resourceId > 0) {// 如果是引用类型的textView.setText(typedArray.getResources().getText(resourceId).toString());} else {// 如果是字符串类型的textView.setText(typedArray.getString(attr));}break;// 处理图片case R.styleable.MyImageView_chanImageViewSrc :resourceId = typedArray.getResourceId(attr, 0);Log.d ("src","---"+resourceId+"---");if (resourceId > 0) {// 是引用类型imageView.setImageResource(resourceId);} else {imageView.setImageResource(R.mipmap.ic_launcher);}break;// 处理方向case R.styleable.MyImageView_chanImageViewOrientation :resourceId = typedArray.getInt (attr,1);Log.d ("orentation","---"+resourceId+"---");if (resourceId == 1) {// 如果是水平this.setOrientation(LinearLayout.HORIZONTAL);} else {// 如果是纵向this.setOrientation(LinearLayout.VERTICAL);}break;}}addView(textView);addView(imageView);typedArray.recycle();}
}

使用完typedArray后记得要调用recycle( );

其中缘由有博客讲的很详细:

http://blog.csdn.net/Monicabg/article/details/45014327

然后在activity_main调用,记得加上命名空间;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"xmlns:my="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.demo.mycustomview.MainActivity"><com.demo.mycustomview.MyImageView
        android:layout_width="wrap_content"android:layout_height="wrap_content"my:chanImageViewTitle="chanImageViewTitle"my:chanImageViewOrientation="Vertical"my:chanImageViewSrc="@mipmap/ic_launcher"></com.demo.mycustomview.MyImageView></LinearLayout>

完结~

这篇关于Iwfu-安卓自定义控件xml属性---TypedArray的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/217753

相关文章

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

滚雪球学Java(87):Java事务处理:JDBC的ACID属性与实战技巧!真有两下子!

咦咦咦,各位小可爱,我是你们的好伙伴——bug菌,今天又来给大家普及Java SE啦,别躲起来啊,听我讲干货还不快点赞,赞多了我就有动力讲得更嗨啦!所以呀,养成先点赞后阅读的好习惯,别被干货淹没了哦~ 🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,助你一臂之力,带你早日登顶🚀,欢迎大家关注&&收藏!持续更新中,up!up!up!! 环境说明:Windows 10

自定义类型:结构体(续)

目录 一. 结构体的内存对齐 1.1 为什么存在内存对齐? 1.2 修改默认对齐数 二. 结构体传参 三. 结构体实现位段 一. 结构体的内存对齐 在前面的文章里我们已经讲过一部分的内存对齐的知识,并举出了两个例子,我们再举出两个例子继续说明: struct S3{double a;int b;char c;};int mian(){printf("%zd\n",s

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

intellij idea generatorConfig.xml

generatorConfig.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-ge

Oracle type (自定义类型的使用)

oracle - type   type定义: oracle中自定义数据类型 oracle中有基本的数据类型,如number,varchar2,date,numeric,float....但有时候我们需要特殊的格式, 如将name定义为(firstname,lastname)的形式,我们想把这个作为一个表的一列看待,这时候就要我们自己定义一个数据类型 格式 :create or repla

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法   消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法 [转载]原地址:http://blog.csdn.net/x605940745/article/details/17911115 消除SDK更新时的“

lvgl8.3.6 控件垂直布局 label控件在image控件的下方显示

在使用 LVGL 8.3.6 创建一个垂直布局,其中 label 控件位于 image 控件下方,你可以使用 lv_obj_set_flex_flow 来设置布局为垂直,并确保 label 控件在 image 控件后添加。这里是如何步骤性地实现它的一个基本示例: 创建父容器:首先创建一个容器对象,该对象将作为布局的基础。设置容器为垂直布局:使用 lv_obj_set_flex_flow 设置容器

HTML5自定义属性对象Dataset

原文转自HTML5自定义属性对象Dataset简介 一、html5 自定义属性介绍 之前翻译的“你必须知道的28个HTML5特征、窍门和技术”一文中对于HTML5中自定义合法属性data-已经做过些介绍,就是在HTML5中我们可以使用data-前缀设置我们需要的自定义属性,来进行一些数据的存放,例如我们要在一个文字按钮上存放相对应的id: <a href="javascript:" d