安卓自定义View

时间:2022-06-17
本文章向大家介绍安卓自定义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>