Android自定義屬性,format

Android自定義屬性-format

string 字符串

  • 定義
    <declare-styleable name="MyTextView">
<!--字符串-->
        <attr name="my_textname" format="string"/>

    </declare-styleable>
  • 獲取
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);

        //獲取文字
        String textName = typedArray.getString(R.styleable.MyTextView_my_textname);

        this.setText(textName);

  • 使用
 <com.liu.myviewformatdemo.view.MyTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:my_textname="無可奈何花落去枯萎地"
            />

dimension 尺寸值

  • 定義
    <declare-styleable name="MyTextView">
<!--字符串-->
        <attr name="my_textname" format="string"/>
        <!--        尺寸值        -->
        <attr name="my_textsize" format="dimension"/>

    </declare-styleable>
  • 獲取
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);

        //獲取文字
        String textName = typedArray.getString(R.styleable.MyTextView_my_textname);

        this.setText(textName);

        //獲取文字大小
        float textsize = typedArray.getDimensionPixelSize(R.styleable.MyTextView_my_textsize, 14);

        this.setTextSize(textsize);

  • 使用
 <com.liu.myviewformatdemo.view.MyTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:my_textname="無可奈何花落去枯萎地"
            app:my_textsize="10sp"
            />

color:顏色值

  • 定義
    <declare-styleable name="MyTextView">
<!--字符串-->
        <attr name="my_textname" format="string"/>
        <!--        尺寸值        -->
        <attr name="my_textsize" format="dimension"/>
        <!--顏色-->
        <attr name="my_textcolor" format="color"/>

    </declare-styleable>
  • 獲取
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);

        //獲取文字
        String textName = typedArray.getString(R.styleable.MyTextView_my_textname);

        this.setText(textName);

        //獲取文字大小
        float textsize = typedArray.getDimensionPixelSize(R.styleable.MyTextView_my_textsize, 14);

        this.setTextSize(textsize);

        //獲取文字顏色
        int color = typedArray.getColor(R.styleable.MyTextView_my_textcolor, 0xff00ff00);
        this.setTextColor(color);

  • 使用
 <com.liu.myviewformatdemo.view.MyTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:my_textname="無可奈何花落去枯萎地"
            app:my_textsize="10sp"
            app:my_textcolor="#f00"
            />

reference:參考某一資源ID。

  • 定義
    <declare-styleable name="MyTextView">
    <!--字符串-->
        <attr name="my_textname" format="string"/>
        <!--        尺寸值        -->
        <attr name="my_textsize" format="dimension"/>
        <!--顏色-->
        <attr name="my_textcolor" format="color"/>
        <!--引用某個資源的ID-->
        <attr name="my_background" format="reference"/>

    </declare-styleable>
  • 獲取
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);

        //獲取文字
        String textName = typedArray.getString(R.styleable.MyTextView_my_textname);

        this.setText(textName);

        //獲取文字大小
        float textsize = typedArray.getDimensionPixelSize(R.styleable.MyTextView_my_textsize, 14);

        this.setTextSize(textsize);

        //獲取文字顏色
        int color = typedArray.getColor(R.styleable.MyTextView_my_textcolor, 0xff00ff00);
        this.setTextColor(color);

        //獲取背景
        int resourceId = typedArray.getResourceId(R.styleable.MyTextView_my_background, R.mipmap.ic_launcher);

        this.setBackgroundResource(resourceId);

  • 使用
 <com.liu.myviewformatdemo.view.MyTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:my_textname="無可奈何花落去枯萎地"
            app:my_textsize="10sp"
            app:my_textcolor="#f00"
            app:my_background="@drawable/bg_shap"
            />
mytextview.png
mytextview.png

boolean:布爾值.

  • 定義
    <declare-styleable name="MyImageView">

        <!--Boolean 是否顯示-->
        <attr name="my_display" format="boolean"/>

    </declare-styleable>
  • 獲取
  • 使用

integer:整型值

  • 定義
<declare-styleable name = "AnimatedRotateDrawable">

    <attr name = "visible" />
    <attr name = "frameDuration" format="integer" />
    <attr name = "framesCount" format="integer" />
    <attr name = "pivotX" />
    <attr name = "pivotY" />
    <attr name = "drawable" />

</declare-styleable>
  • 獲取
  typedArray.getInteger(R.styleable.名稱,默認值)
  • 使用
<animated-rotate
    xmlns:android ="http://schemas.android.com/apk/res/android"
    android:drawable = "@drawable/圖片ID"
    android:pivotX = "50%"
    android:pivotY = "50%"
    android:framesCount = "12"
    android:frameDuration = "100"/>

float:浮點值

  • 定義
<declare-styleable name = "AlphaAnimation">

    <attr name = "fromAlpha" format = "float" />
    <attr name = "toAlpha" format = "float" />

</declare-styleable>
  • 獲取
typedArray.getFloat(R.styleable.名稱,默認值)
  • 使用
<alpha
    android:fromAlpha = "1.0"
    android:toAlpha = "0.7"/>

fraction:百分數

  • 定義
<declare-styleable name="RotateDrawable">
  <attr name = "visible" />
  <attr name = "fromDegrees" format = "float" />
  <attr name = "toDegrees" format = "float" />
  <attr name = "pivotX" format = "fraction" />
  <attr name = "pivotY" format = "fraction" />
  <attr name = "drawable" />
</declare-styleable>
  • 使用
<rotate
    xmlns:android ="http://schemas.android.com/apk/res/android"
    android:interpolator = "@anim/動畫ID"
    android:fromDegrees = "0"
    android:toDegrees = "360"
    android:pivotX = "200%"
    android:pivotY = "300%"
    android:duration = "5000"
    android:repeatMode = "restart"
    android:repeatCount = "infinite"
/>

enum:枚舉值

  • 定義
<declare-styleable name="名稱">
    <attr name="orientation">
    <enum name="horizontal" value="0" />
    <enum name="vertical" value="1" />
    </attr>
</declare-styleable>
  • 使用
<LinearLayout
    xmlns:android = "http://schemas.android.com/apk/res/android"
    android:orientation = "vertical"
    android:layout_width = "fill_parent"
    android:layout_height = "fill_parent" >
</LinearLayout>

flag:位或運算

  • 定義
<declare-styleable name="名稱">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
<flag name = "stateVisible" value = "4" />
<flag name = "stateAlwaysVisible" value = "5" />
<flag name = "adjustUnspecified" value = "0x00" />
<flag name = "adjustResize" value = "0x10" />
<flag name = "adjustPan" value = "0x20" />
<flag name = "adjustNothing" value = "0x30" />
</attr>

</declare-styleable>
  • 使用
<activity
    android:name = ".StyleAndThemeActivity"
    android:label = "@string/app_name"
    android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
    <intent-filter>
        <action android:name = "android.intent.action.MAIN" />
        <category android:name = "android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

屬性定義時可以指定多種類型值

<declare-styleable name = "名稱">

<attr name = "background" format = "reference|color" />

</declare-styleable>

個人主頁:

https://ln0491.github.io/
http://ln0491.coding.me/
博客:
http://blog.csdn.net/ko0491
http://www.cnblogs.com/liunanjava/

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1. reference:參考某一資源ID。(1)屬性定義: (2)屬性使用: 2. color:顏色值。(1)屬...
    ShereenAo閱讀 786評論 0 0
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,420評論 25 708
  • 在values文件下有一個文件就是attrs.xml,這是R.attr和R.styleable類的定義文件。att...
    于闐閱讀 4,528評論 1 4
  • 昨晚為一個即將離開武漢去上海的朋友踐行,三對男女的晚餐少不了瞎侃,其中一位女生說到很喜歡孫悟空,覺得孫悟空...
    迷路的小熊閱讀 241評論 0 0
  • 讓我回憶一下這一周都有哪些大事發生 浪潮之巔進展很不錯,雖然沒有讀完,但上下兩冊呢,還有100多頁,真是本好書,I...
    筱筱殿下閱讀 262評論 0 0