安卓自定義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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章