定義自己的ViewGroup,定義自己ViewGroup
尊重原創:http://blog.csdn.net/yuanzeyao/article/details/40264433
好久都沒有寫文章了,現在利用周末的時間對一些知識進行總結,便於加深理解,今天我就來學習一下如何?自訂ViewGroup
在Android中比較常用的布局LinearLayout,FrameLayout,RelativeLayout。。。這些布局都是繼承自ViewGroup,通過這些布局,我們幾乎可以實現Android中所有的介面開發,但是對於一些比較常見並且比較複雜的布局,使用這些基本布局開發的話,就會花大量的時間在一些重複的工作上,那麼我們能不能模仿這些基本布局,根據自己的需求來實現一些自己的布局,以後需要的時候直接拿來用呢?當然是可以的。
類似撲克牌的布局
在鬥地主的遊戲中,我們會經常遇到類似這樣的布局,我就將這個布局叫級聯布局吧,這個布局使用RelativeLayout和margin等熟悉是可以做出來的,但是不是很方便,我們今天就這種需求為背景講解一下自訂ViewGroup
在學習自訂布局前,讀者最好先瞭解一下Android的布局好似怎麼繪畫出來的,我推薦大家去瞭解一下:都是官網的一些文章
1、http://developer.android.com/guide/topics/ui/how-android-draws.html
2、http://developer.android.com/reference/android/view/ViewGroup.html
3、http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
通過以上幾篇文章,我們需要瞭解一下知識:
1、繪製布局是由兩個過程組成:測量過程和版面配置階段,測量過程使用measure(int,int)方法完成,遍曆完成後,所有的View的大小都確定了,版面配置階段使用layout方法完成,就是通過View的大小,決定View放置在上面地方,不過通過源碼中measure和layout方法都是final類型的,所以我們是無法改寫的,之所以定義成final的,就是避免開發人員破壞了布局的繪畫流程,不過測量和布局的細節我們可以通過改寫onMeasure和onLayout實現。
2、ViewGroup
在前面我就說過,Android中所有的布局都是繼承自ViewGroup,ViewGroup就是一個View的容器,我們可以再裡面放置任務的View,至於如何放置,我們可以通過onMeasure和onLayout來定義,onMeasure在measure調用,onLayout是在layout中調用的
3、ViewGroup.LayoutParams
這個類主要是View用來告訴他的父容器它想怎麼顯示,如寬、高、置中等等。ViewGroup.LayoutParams裡面最重要的兩個參數就是width,height。如果你想使用margin屬性,那麼必須使用ViewGroup.MarginLayoutParams這個類,這個類繼承自ViewGroup.LayoutParams。添加了對margin屬性的支援,如果你想加入更多的屬性,可以自己定義一個LayoutParams類加入你需要的屬性,事實上LinearLayout等布局都是繼承自ViewGroup.MarginLayoutParams,並加入了自己需要的屬性。
實現自己的布局CascadeLayout.java
/** * 自訂布局,用來實現撲克牌效果 * com.myviewgroup.CascadeLayout * @author yuanzeyao <br/> * create at 2014年10月19日 下午4:15:42 */public class CascadeLayout extends ViewGroup{ /** * 水平位移距離 */ private int horizontal_space; /** * 垂直位移距離 */ private int vertical_space; public CascadeLayout(Context context) { super(context); } public CascadeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initAttribute(context, attrs); } public CascadeLayout(Context context, AttributeSet attrs) { super(context, attrs); initAttribute(context, attrs); } /** * 根據xml檔案中的屬性對horizontal_space,vertical_space來賦值 * @param context * @param attrs */ private void initAttribute(Context context,AttributeSet attrs) { TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.CascadeLayout); horizontal_space=a.getDimensionPixelSize(R.styleable.CascadeLayout_horizontal_space,this.getResources().getDimensionPixelSize(R.dimen.cascade_horizontal_spacing)); vertical_space=a.getDimensionPixelSize(R.styleable.CascadeLayout_vertical_space, this.getResources().getDimensionPixelSize(R.dimen.cascade_vertical_spacing)); a.recycle(); } /** * onMeasure在measure中調用,參數分別是CascadeLayout的寬度和高度 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //使用measureChildren對所有的孩子進行測量,你也可以使用for迴圈,然後掉哦弄個measurechild函數進行測量 measureChildren(widthMeasureSpec, heightMeasureSpec); //將寬度和高度賦值為CascadeLayout的measureWidth,measureHeight變數 setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); } /** * 在該函數中添加如何布置各個View的邏輯 */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { for(int i=0;i<getChildCount();i++) { //依次遍曆孩子節點 View child=getChildAt(i); // MarginLayoutParams mlp=(MarginLayoutParams) child.getLayoutParams(); LayoutParams lp=child.getLayoutParams(); //計算左邊和頂部的距離 int left=horizontal_space*i; int top=vertical_space*i; child.layout(left,top,left+child.getMeasuredWidth(),top+child.getMeasuredHeight()); } } /** * 改寫產生LayoutParams的方法 */ @Override protected LayoutParams generateLayoutParams(LayoutParams p) { //return super.generateLayoutParams(p); //return new ViewGroup.MarginLayoutParams(p.width,p.height); return new ViewGroup.LayoutParams(p.width,p.height); } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { //return super.generateLayoutParams(attrs); //return new ViewGroup.MarginLayoutParams(this.getContext(), attrs); return new ViewGroup.LayoutParams(this.getContext(), attrs); } }
在CascadeLayout中,我們定義了兩個屬性,horizontal_space,vertical_space,這兩個屬性用來記錄該布局中每個子View之前的垂直距離和水平距離,他們都是在建構函式中初始化。由於我們添加了屬性,所以在res/values中定義attrs.xml檔案
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="CascadeLayout"> <attr name="horizontal_space" format="dimension"></attr> <attr name="vertical_space" format="dimension"></attr> </declare-styleable></resources>
在layout檔案中使用CascadeLayout
<com.myviewgroup.CascadeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cascade ="http://schemas.android.com/apk/res/com.myviewgroup" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" cascade:horizontal_space="40dip" cascade:vertical_space="40dip" > <View android:layout_width="200dip" android:layout_height="200dip" android:background="@color/view1" /> <View android:layout_width="200dip" android:layout_height="200dip" android:background="@color/view2" /> <View android:layout_width="200dip" android:layout_height="200dip" android:background="@color/view3" /></com.myviewgroup.CascadeLayout>
運行效果如下
自訂自己的LayoutParams在CascadeLayout中,我們使用的是ViewGroup.LayoutParams類,但是這個類僅僅有width和height屬性,如果我們想加入其他屬性,那麼我們需要自己定義LayoutParams類
下面我們自訂自己的LayoutParams類,這個類繼承ViewGroup.MarginLayoutParams,所以我自訂的LayoutPrams具有margin的屬性
在CascadeLayout中定義如下LayoutParams類
public static class LayoutParams extends ViewGroup.MarginLayoutParams { /** * 定義在垂直方向的位移距離,可以覆蓋CascadeLayout中的vertical_space屬性 */ private int layout_vertical_spacing; public LayoutParams(Context c, AttributeSet attrs) { super(c, attrs); TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.CascadeLayout_LayoutParams); try { layout_vertical_spacing = a .getDimensionPixelSize( R.styleable.CascadeLayout_LayoutParams_layout_vertical_spacing, -1); } finally { a.recycle(); } } public LayoutParams(int width, int height) { super(width, height); } }該LayoutParams就支援了layout_vertical_spaceing屬性了。
好了,先寫到這裡吧,有什麼不懂的歡迎大家留言。。。
自訂ViewGroup
在ViewGroup的onMeasure方法中,先對TextView調用measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)方法,之後再調用TextView的getMeasuredHeight()得出TextView的高,調用TextView的getMeasuredWidth()得出TextView的寬。
自訂viewgroup問題
搞定了,點擊空白處,onInterceptTouchEvent在down事件時返回了false,然後將事件遞交給子view處理,由於空白處無子view,因此事件又回到viewgroup的onTouchEvent回調中,而我在viewgroup的onTouchEvent的down事件處返回了false,因此後續的move和up事件都遞交給此viewgroup的parent view來處理了,viewgroup自己收不到了。所以出現了這個問題。解決方案就是在viewgroup的onTouchEvent的down事件處返回true。哈哈哈,我自問自答了