安卓自定义View

attrs.xml 详解

我们在使用组件属性的时候经常会使用

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减少分数" 

这些都是系统属性,我们通过attrs.xml可以自己定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    //自定义属性名,定义公共属性
    //name=“样式属性名称” format="样式类型如:String、int、color等"
    <attr name="titleSize" format="dimension"></attr>
    <attr name="titleText" format="string"></attr>
    <attr name="titleColor" format="color"></attr>
    <attr name="outCircleColor" format="color"></attr>
    <attr name="inCircleColor" format="color"></attr>
    <attr name="lineColor" format="color"></attr>

    //自定义控件的主题样式
    //MySportView样式名称
    <declare-styleable name="MySportView">
        <attr name="titleSize"></attr>
        <attr name="titleText"></attr>
        <attr name="titleColor"></attr>
        <attr name="outCircleColor"></attr>
        <attr name="inCircleColor"></attr>
    </declare-styleable>
   
    <declare-styleable name="MyQQHealthView">
        <attr name="titleColor"></attr>
        <attr name="lineColor"></attr>
    </declare-styleable>
</resources>

扫码关注公众号“伟大程序猿的诞生“,更多干货新鲜文章等着你~

公众号回复“资料获取”,获取更多干货哦~

有问题添加本人微信号“fenghuokeji996” 或扫描博客导航栏本人二维码

发布了97 篇原创文章 · 获赞 961 · 访问量 49万+

猜你喜欢

转载自blog.csdn.net/github_33304260/article/details/52020701