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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CTS测试介绍(面试怎么介绍接口测试)

    CTS测试基本介绍CTS测试全称为系列兼容测试(CompatibilityTestsuite),CTS是为了测试手机是否符合google定义的兼容性规范(CompatibilityDefinition)。从而基于Android的应用程序能在基于同一个api版本的设备上面运行。通过CTS测试的设备可以获得Android的商标,并且享受AndroidMarket的权限。CTS测试是一个基于…

    2022年4月10日
    62
  • 常量表达式概念与用处的关系_常量和变量有什么异同点

    常量表达式概念与用处的关系_常量和变量有什么异同点1)什么叫常量表达式?        在编译期间进行求值的表达式。    1、字面值常量是常量表达式;如123,‘a’,3.14等    2、用常量表达式初始化的const对象也是常量表达式。如intconsta=5;语句中a就是常量表达式2)什么地方会用到常量表达式?        就目前所知道的有三个,后面如果发现再新增。

    2022年9月29日
    0
  • kettle 教程(四):自定义 Java 代码

    kettle 教程(四):自定义 Java 代码kettle拥有很多自带的组件,能帮我们实现很多的功能。但是我们总有一些很复(qi)杂(pa)的需求,用自带的组件实现不了,或者说实现起来很复杂。那么这时我们就要用到万能的组件了(Java代码),通过自己写代码来实现任何想要的功能。自定义Java代码假设有这样一个需…

    2022年5月23日
    240
  • ES6模板字符串`的引用

    ES6模板字符串`的引用传统的JavaScript语言,输出模板通常是这样写的(下面使用了jQuery的方法)。$(‘#result’).append(‘Thereare<b>’+basket.count+'</b>’+’itemsinyourbasket,’+'<em>’+basket.onSale+'</em>areonsale!’);上面这种写法相当繁琐不方便,ES6引入了模板字符串解决这个问题。

    2022年8月21日
    10
  • Java动态拼接SQL–02–Jpa「建议收藏」

    Java动态拼接SQL–02–Jpa「建议收藏」本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求。这里不讲解JPA和Spring-data-jpa单独使用,所有的内容都是在和Spring整合的环境中实现。如果需要了解该框架的入门,百度一下,很多入门的介绍。在这篇文章的接下来一篇,会有一个系列来讲解mybatis,这个系列从mybatis的入门开始,到基本使用,和spring整合,和第

    2022年6月10日
    254
  • jupyter如何运行代码_python jupyter notebook安装

    jupyter如何运行代码_python jupyter notebook安装python-mpipinstall–upgradepippython3-mpipinstall–upgradepip

    2022年10月8日
    0

发表回复

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

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