declare-styleable使用

declare-styleable使用declare-styleable是给自定义控件添加自定义属性用的attr中在attrs.xml中设置declare-styleable,name是PersonAttr<?xmlversion=”1.0″encoding=”utf-8″?><resources><declare-styleablename=”PersonAttr”><attrname=”name”format=”reference”/&gt

大家好,又见面了,我是你们的朋友全栈君。

declare-styleable是给自定义控件添加自定义属性用的

attr中

在attrs.xml中设置declare-styleable,name是PersonAttr

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="PersonAttr">  
        <attr name="name" format="reference" />  
        <attr name="age" format="integer" />  
        <attr name="weight">  
            <flag name="fat" value="2" />  
            <flag name="mid" value="1" />  
            <flag name="thin" value="0" />  
        </attr>  
        <attr name="adult" format="boolean" />  
        <attr name="textSize" format="dimension" />  
    </declare-styleable>  
</resources> 

format就是格式,里面的就是这个属性对应的格式,下面列出来大致的格式有:

  1. reference:参考某一资源ID,以此类推
    属性定义:
<declare-styleable name = "名称">
	<attr name = "background" format = "reference" />
</declare-styleable>

属性使用:

<ImageView
	android:layout_width = "42dip"
	android:layout_height = "42dip"
	android:background = "@drawable/图片ID"
	/>
  1. color:颜色值
  2. boolean:布尔值
  3. dimension:尺寸值。注意,这里是像素
  4. float:浮点值
  5. integer:整型值
  6. string:字符串
  7. fraction:百分数
  8. enum:枚举值
  9. flag:是自己定义的
  10. reference|color:颜色的资源文件
  11. reference|boolean:布尔值的资源文件

由于reference是从资源文件中获取,所以在XML文件中写这个属性的时候必须 personattr:name="@string/app_name"这种格式,否则会出错

自定义View中

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

public class PersonView extends AppCompatTextView { 
   
    public PersonView(Context context) { 
   
        super(context);
        // TODO Auto-generated constructor stub
    }

    public PersonView(Context context, AttributeSet attrs, int defStyle) { 
   
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public PersonView(Context context, AttributeSet attrs) { 
   
        super(context, attrs);
        // TODO Auto-generated constructor stub
        TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.PersonAttr);//获取配置属性
        String name = tArray.getString(R.styleable.PersonAttr_name);
        int age = tArray.getInt(R.styleable.PersonAttr_age, 15);
        int weight = tArray.getInt(R.styleable.PersonAttr_weight, 1);// 默认是中等身材,属性为:1
        String str_weight = getWeightStatus(weight);//获得肥胖属性

        Boolean adult = tArray.getBoolean(R.styleable.PersonAttr_adult, false);
        String str_adult = getAdultStatus(adult);

        float textSize = tArray.getDimension(R.styleable.PersonAttr_textSize, 15);
        setTextSize(textSize);//设置字体大小

        tArray.recycle();//回收资源

        setText("姓名:" + name + "\n" + "年龄:" + age + "\n" + "是否成年:" + str_adult
                + "\n" + "体型:" + str_weight);//给自定义的控件赋值
    }

    /** * 根据传入的值判断是否成年 */
    public String getAdultStatus(Boolean adult) { 
   
        String str_adult = "未成年";
        if (adult) { 
   
            str_adult = "成年";
        }
        return str_adult;
    }

    /** * 根据传入的值判断肥胖状态 */
    public String getWeightStatus(int weight) { 
   
        String str_weight = "中等";
        switch (weight) { 
   
            case 0:
                str_weight = "瘦";
                break;
            case 1:
                str_weight = "中等";
                break;
            case 2:
                str_weight = "肥胖";
                break;
            default:
                break;
        }
        return str_weight;
    }
}

xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:personattr="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.xx.testapplication.PersonView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        personattr:adult="false"
        personattr:name="@string/person_name"
        personattr:weight="thin" 
        personattr:textSize="15px"/>
</LinearLayout>

注意根布局增加的

xmlns:personattr="http://schemas.android.com/apk/res-auto"

结构是

xmlns:xml中使用的属性前缀="http://schemas.android.com/apk/res-auto"

结果
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158524.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月13日 下午12:00
下一篇 2022年7月13日 下午12:00


相关推荐

  • BIEE-1 初始化块和变量[通俗易懂]

    BIEE-1 初始化块和变量[通俗易懂]一、初始化块biee初始化块分为资料库、会话两类资料库初始化块:可批量同事定义变量的值配置:1.“编辑数据源”写入sql并分配地址池;2.“编辑数据目标”配置变量,变量的值和初始化sql结果是按顺序匹配的,一对一关系;3.“刷新时间间隔”默认是1小时,即每个一小时系统就会自动执行一遍此初始化块语句,并把结果存在缓冲池中,用户登录系统时,从缓冲池中

    2025年5月24日
    4
  • IDEA官方汉化包

    IDEA官方汉化包IDEA修改为中文模式自IEDA2020发行不久,IDEA官方就发布了支持中文喜讯。今晚看到同学还在为找到IDEA汉化包而高兴,不由得想到可以水一篇博客。这是我用官方的包汉化IDEA2020.1后的结果:基本上能看到的英文字母都汉化成简体中文了,就很nice!汉化过程很简单Ctrl+Alt+s进入setting找到Plugins,直接在如下的搜索框中输入chinese回车,选择第一个如图所示插件进行下载并应用。然后重启即可发现:诶!英文变成中文了。也可以在官网下载jar包导入IDEA安装

    2022年6月9日
    295
  • OpenClaw使用教程(免费版)

    OpenClaw使用教程(免费版)

    2026年3月15日
    2
  • 技能系统 | OpenClaw 中文文档

    技能系统 | OpenClaw 中文文档

    2026年3月12日
    4
  • 如何求逆矩阵_副对角线矩阵的逆矩阵怎么求

    如何求逆矩阵_副对角线矩阵的逆矩阵怎么求作为一只数学基础一般般的程序猿,有时候连怎么求逆矩阵都不记得,之前在wikiHow上看了一篇不错的讲解如何求3×3矩阵的逆矩阵的文章,特转载过来供大家查询以及自己备忘。当然这个功能在matlab里面非常容易实现,只要使用inv函数或A^-1即可,但是有时候参加个考试什么的还是要笔算的哈哈~假设有如下的3×3矩阵,第一步需要求出det(M),也就是矩阵M的行列式的值。行列式的值通常显示

    2022年8月21日
    15
  • MyBatis框架核心之(五)注解使用resultMap及多表查询「建议收藏」

    MyBatis框架核心之(五)注解使用resultMap及多表查询「建议收藏」MyBatis框架核心之(五)注解使用resultMap及多表查询

    2022年4月21日
    47

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号