前言:上个月实在是太忙了,根本没时间看看书,博客也只写了一篇,还好这个月能稍微轻松一点,不然真是要被虐死的节奏,其实我也挺容易满足,只要能给我时间看看书,无论生活有多累,突然间就会幸福感爆棚。哈,程序猿真是一个奇怪的动物。
相关文章:
1、《PullScrollView详解(一)——自定义控件属性》
2、《PullScrollView详解(二)——Animation、Layout与下拉回弹》
3、《PullScrollView详解(三)——PullScrollView实现》
4、《PullScrollView详解(四)——完全使用listview实现下拉回弹(方法一)》
5、《PullScrollView详解(五)——完全使用listview实现下拉回弹(方法二)》
6、《PullScrollView详解(六)——延伸拓展(listview中getScrollY()一直等于0、ScrollView中的overScrollBy)》
这里又将介绍另一个控件了——PullScrollView,同样是利用几篇来详细讲述它的实现方法,先看看最终的效果吧:
其实效果也挺简单,下拉回弹,然后在下拉的时候顶部的图片伸展。
看似简单的效果,用到的知识可不少,今天就先给大家做个铺垫,讲讲如何给自定义控件添加上自定义的控件属性。
平时大家在看别人写的代码时,可能在一个自定义控件的XML中也发现过类似的代码:
大家看最后三个属性:
attrstest:headerHeight="300dp" attrstest:headerVisibleHeight="100dp" attrstest:age ="young"明显这三个属性不是系统自带的,这是自己添加上去的。那怎么添加自定义的属性呢?利用XML中的declare-styleable标签来做。
public class MyTextView extends TextView { public MyTextView(Context context) { super(context); } }
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyTextView"> <attr name="header" format="reference" /> <attr name="headerHeight" format="dimension" /> <attr name="headerVisibleHeight" format="dimension" /> <attr name="age"> <flag name="child" value="10"/> <flag name="young" value="18"/> <flag name="old" value="60"/> </attr> </declare-styleable> </resources>注意:
<com.harvic.com.trydeclarestyle.MyTextView android:layout_width="fill_parent" android:layout_height="match_parent" attrstest:header="@drawable/pic1" attrstest:headerHeight="300dp" attrstest:headerVisibleHeight="100dp" attrstest:age="young"/>可以看到header的取值是从其它XML引用过来的;dimension就表示尺寸,直接输入数字;flag就相当于代码里的常量,比如这里的young就表示数字18
(1)、添加自定义控件
我们在一个XML布局中,比如下面这个activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.harvic.com.trydeclarestyle.MyTextView android:layout_width="fill_parent" android:layout_height="match_parent" attrstest:header="@drawable/pic1" attrstest:headerHeight="300dp" attrstest:headerVisibleHeight="100dp" attrstest:age="young"/> </RelativeLayout>如果我们像上面这样,直接添加自定义的控件及属性,你会发现,所有的自定义属性都会标红!这是因为这个XML根本识别不了这些标记
要让它识别我们自定义的属性也非常简单,在根布局上添加
xmlns:attrstest ="http://schemas.android.com/apk/res/com.harvic.com.trydeclarestyle"这里有两点注意:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.harvic.com.trydeclarestyle" >所以,在这种方式下,完整的activity_main..xml代码应该是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:attrstest ="http://schemas.android.com/apk/res/com.harvic.com.trydeclarestyle" android:layout_width="match_parent" android:layout_height="match_parent"> <com.harvic.com.trydeclarestyle.MyTextView android:layout_width="fill_parent" android:layout_height="match_parent" attrstest:headerHeight="300dp" attrstest:headerVisibleHeight="100dp" attrstest:age="young"/> </RelativeLayout>(3)、导入自定义的属性集(方法二)
xmlns:attrstest="http://schemas.android.com/apk/res-auto"其中xmlns:attrstest,这里的attrstest是自定义的,你想定义成什么就可以定义成什么。但要注意的是,下面访问你定义的XML控件属性时,就是通过这个标识符访问的。比如我们这里定义成attrstest;那我们下面对应的访问自定义控件的方式就是:attrstest:headerHeight="300dp";
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:attrstest="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.harvic.com.trydeclarestyle.MyTextView android:layout_width="fill_parent" android:layout_height="match_parent" attrstest:headerHeight="300dp" attrstest:headerVisibleHeight="100dp" attrstest:age="young"/> </RelativeLayout>
typedArray.getInt(int index, float defValue); typedArray.getDimension(int index, float defValue); typedArray.getBoolean(int index, float defValue); typedArray.getColor(int index, float defValue); typedArray.getString(int index) typedArray.getDrawable(int index); typedArray.getResources();我们就看看在MyTextView中获取我们在XML中定义的那些属性的值,然后将它设置到成显示的文字:
public class MyTextView extends TextView { public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyTextView); float headerHeight = typedArray.getDimension(R.styleable.MyTextView_headerHeight,-1); int age = typedArray.getInt(R.styleable.MyTextView_age,-1); typedArray.recycle(); this.setText("headerHeight:"+headerHeight + " age:"+age); } }整个工程的效果图如下:
好了,到这里基本上就结束了,下面就讲讲declare-styleable标签下其它属性的用法。
源码在文章底部给出
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "background" format = "reference" /> </declare-styleable>(2)属性使用:
<ImageView android:layout_width = "42dip" android:layout_height = "42dip" android:background = "@drawable/图片ID"/>
<declare-styleable name = "名称"> <attr name = "textColor" format = "color" /> </declare-styleable>(2)属性使用:
<TextView android:layout_width = "42dip" android:layout_height = "42dip" android:textColor = "#00FF00"/>
<declare-styleable name = "名称"> <attr name = "focusable" format = "boolean" /> </declare-styleable>(2)属性使用:
<Button android:layout_width = "42dip" android:layout_height = "42dip" android:focusable = "true"/>
<declare-styleable name = "名称"> <attr name = "layout_width" format = "dimension" /> </declare-styleable>(2)属性使用:
<Button android:layout_width = "42dip" android:layout_height = "42dip"/>
<declare-styleable name = "AlphaAnimation"> <attr name = "fromAlpha" format = "float" /> <attr name = "toAlpha" format = "float" /> </declare-styleable>(2)属性使用:
<alpha android:fromAlpha = "1.0" android:toAlpha = "0.7"/>
<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>(2)属性使用:
<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"/>
<declare-styleable name = "MapView"> <attr name = "apiKey" format = "string" /> </declare-styleable>(2)属性使用:
<com.google.android.maps.MapView android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />
<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>(2)属性使用:
<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"/>
<declare-styleable name="名称"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable>(2)属性使用:
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent"> </LinearLayout>
<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>(2)属性使用:
<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>(2)属性使用:
<ImageView android:layout_width = "42dip" android:layout_height = "42dip" android:background = "@drawable/图片ID|#00FF00"/>
参考文章:
1、《declare-styleable的使用(自定义控件) 以及declare-styleable中format详解》
2、《Android 自定义UI View - 04 圆形图片控件之自定义属性》
如果本文有帮到您,记得加关注哦
源码下载地址:http://download.csdn.net/detail/harvic880925/8817777
请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/46537767 谢谢