前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >declare-styleable使用

declare-styleable使用

作者头像
全栈程序员站长
发布2022-09-14 09:51:55
3660
发布2022-09-14 09:51:55
举报
文章被收录于专栏:全栈程序员必看

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

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

attr中

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

代码语言:javascript
复制
<?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,以此类推 属性定义:
代码语言:javascript
复制
<declare-styleable name = "名称">
	<attr name = "background" format = "reference" />
</declare-styleable>

属性使用:

代码语言:javascript
复制
<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中

代码语言:javascript
复制
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中

代码语言:javascript
复制
<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>

注意根布局增加的

代码语言:javascript
复制
xmlns:personattr="http://schemas.android.com/apk/res-auto"

结构是

代码语言:javascript
复制
xmlns:xml中使用的属性前缀="http://schemas.android.com/apk/res-auto"

结果

在这里插入图片描述
在这里插入图片描述

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • attr中
  • 自定义View中
  • xml中
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档