您的位置:首页 > 其它

自定义ViewPagerIndicator(下)

2015-09-08 22:14 489 查看
由于有些参数定义为成员变量,不好复制,具体请参考源码。

(1) 在values目录中创建attrs.xml( 技巧:拷贝ApiDemos\res\values\attrs.xml,保留labelview,删除其他进行编辑比较快)

<resources>

<declare-styleable name="PagerIndicator">
<attr name="selectedColor" format="color" />
<attr name="strokeWidth" format="dimension" />
<attr name="unselectedColor" format="color" />
<attr name="radius" format="dimension" />
</declare-styleable>
</resources>


(2) 在自定义控件两个参数构造方法中解析属性,并设置到画笔和画布上

public PagerIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.PagerIndicator);
colorSelect = a.getColor(
R.styleable.PagerIndicator_selectedColor, Color.GRAY);
colorUnselect = a.getColor(
R.styleable.PagerIndicator_unselectedColor, Color.RED);
strokeWidth = a.getDimension(
R.styleable.PagerIndicator_strokeWidth, 3);
radius = a.getDimension(
R.styleable.PagerIndicator_radius, 15);
a.recycle();// 释放内存
initPaint();
}


(3)在需要此控件的布局上使用它

<com.xinbo.demo.PagerIndicator
xmlns:indicator="http://schemas.android.com/apk/res-auto"
android:id="@+id/pagerIndicator1"
indicator:selectedColor="#0000FF"
indicator:unselectedColor="#00FF00"
indicator:strokeWidth="1dp"
indicator:radius="10dp"
android:layout_width="match_parent"
android:background="#40000000"
android:layout_gravity="bottom"
android:layout_height="80dp" />


源码地址:http://download.csdn.net/download/sjc1010592550/9090189
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: