android之自訂ViewGroup和自動換行的布局的實現

來源:互聯網
上載者:User

  viewgroup簡單說就是可以裝view的view.今天遇到一個問題,就是需要一個可以自動根據一行中view的寬度自動換行的布局,網上找了下,沒有相關的例子,但是找到了思路:自訂一個viewgroup,然後在onlayout檔案裡面自動檢測view的右邊緣的橫座標值,和你的view的parent view的況度判斷是否換行顯示view就可以了。因為代碼比較簡單,就不多說了:

  

 1 public class MyViewGroup extends ViewGroup {
2 private final static String TAG = "MyViewGroup";
3
4 private final static int VIEW_MARGIN=2;
5
6 public MyViewGroup(Context context) {
7 super(context);
8 }
9 @Override
10 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11 Log.d(TAG, "widthMeasureSpec = "+widthMeasureSpec+" heightMeasureSpec"+heightMeasureSpec);
12
13 for (int index = 0; index < getChildCount(); index++) {
14 final View child = getChildAt(index);
15 // measure
16 child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
17 }
18
19 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
20 }
21
22 @Override
23 protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
24 Log.d(TAG, "changed = "+arg0+" left = "+arg1+" top = "+arg2+" right = "+arg3+" botom = "+arg4);
25 final int count = getChildCount();
26 int row=0;// which row lay you view relative to parent
27 int lengthX=arg1; // right position of child relative to parent
28 int lengthY=arg2; // bottom position of child relative to parent
29 for(int i=0;i<count;i++){
30
31 final View child = this.getChildAt(i);
32 int width = child.getMeasuredWidth();
33 int height = child.getMeasuredHeight();
34 lengthX+=width+VIEW_MARGIN;
35 lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2;
36 //if it can't drawing on a same line , skip to next line
37 if(lengthX>arg3){
38 lengthX=width+VIEW_MARGIN+arg1;
39 row++;
40 lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2;
41
42 }
43
44 child.layout(lengthX-width, lengthY-height, lengthX, lengthY);
45 }
46
47 }
48
49 }

  這裡有個地方要注意,那就要明白ViewGroup的繪圖流程:ViewGroup繪製包括兩個步驟:1.measure 2.layout

  在兩個步驟中分別調用回呼函數:1.onMeasure()   2.onLayout()

  1.onMeasure() 在這個函數中,ViewGroup會接受childView的請求的大小,然後通過childView的 measure(newWidthMeasureSpec, heightMeasureSpec)函數儲存到childView中,以便childView的getMeasuredWidth() andgetMeasuredHeight() 的值可以被後續工作得到。

  2.onLayout() 在這個函數中,ViewGroup會拿到childView的getMeasuredWidth() andgetMeasuredHeight(),用來布局所有的childView。

  3.View.MeasureSpec
與 LayoutParams
這兩個類,是ViewGroup與childView協商大小用的。其中,View.MeasureSpec是ViewGroup用來部署
childView用的, LayoutParams是childView告訴ViewGroup 我需要多大的地方。

  4.在View 的onMeasure的最後要調用setMeasuredDimension()這個方法儲存View的大小,這個方法決定了當前View的大小。

  

  :

                                 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.