android 自定义 styleable 属性

1.定义 

attr.xml中定义 

<resources>
    <declare-styleable name="ViewFlow">
        <attr name="sidebuffer" format="integer" />
    </declare-styleable>
    <declare-styleable name="CircleFlowIndicator">
        <attr name="activeColor" format="color" />
        <attr name="inactiveColor" format="color" />
        <attr name="radius" format="dimension" />
        <attr name="spacing" format="dimension" />
        <attr name="centered" format="boolean" />
        <attr name="fadeOut" format="integer" />
        <attr name="inactiveType">
            <flag name="stroke" value="0" />
            <flag name="fill" value="1" />
        </attr>
        <attr name="activeType">
            <flag name="stroke" value="0" />
            <flag name="fill" value="1" />
        </attr>
        <attr name="snap" format="boolean" />
    </declare-styleable>    
    <declare-styleable name="TitleFlowIndicator">
        <attr name="titlePadding" format="dimension" />
        <!-- Left/right padding of not active view titles. -->
        <attr name="clipPadding" format="dimension" />
        <attr name="selectedColor" format="color" />
        <attr name="selectedBold" format="boolean" />
        <attr name="selectedSize" format="dimension" />
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
        <attr name="footerLineHeight" format="dimension" />
        <attr name="footerColor" format="color" />
        <attr name="footerTriangleHeight" format="dimension" />
        <attr name="customTypeface" format="string" />
    </declare-styleable>    
</resources>

相关字段

resources -> declare-styleable ->attr( name ,format (color,dimension,boolean,string,integer,))  or -> flag(name,value)

2.layout中的使用

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/org.taptwo.android.widget.viewflow.example"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <org.taptwo.android.widget.CircleFlowIndicator
        android:padding="10dip"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/viewflowindic"
        android:layout_gravity="center_horizontal"
        app:inactiveType="fill"
        app:fadeOut="1000" />
    <org.taptwo.android.widget.ViewFlow
        android:id="@+id/viewflow" android:layout_width="fill_parent"
        android:layout_height="fill_parent" app:sidebuffer="3"></org.taptwo.android.widget.ViewFlow>

</FrameLayout>

  1.xmlns:app="http://schemas.android.com/apk/res/org.taptwo.android.widget.viewflow.example"   

  app为自定义标签前缀 路径为 http://schemas.android.com/apk/res + 资源路径(包名)

  2.app:inactiveType="fill"   app:fadeOut="1000"  app:textColor="#FFFFFFFF"  app:footerTriangleHeight="10dp" app:customTypeface="fonts/Antic.ttf"

 如上定义  inactivieType是自定义类型 fadeOut是integer类型 textColor是color类型 footerTriangleHeight是dimension类型  customTypeface是string类型
3.view中读取
    TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.CircleFlowIndicator);

        // Gets the active circle type, defaulting to "fill"
        int activeType = a.getInt(R.styleable.CircleFlowIndicator_activeType,
                STYLE_FILL);
        
        int activeDefaultColor = 0xFFFFFFFF;
        
        // Get a custom active color if there is one
        int activeColor = a
                .getColor(R.styleable.CircleFlowIndicator_activeColor,
                        activeDefaultColor);

如上 1.obtion TypedArray 2.getInt getColor 等

posted on 2015-04-26 12:46  wjw334  阅读(1839)  评论(0编辑  收藏  举报

导航