流式布局的實現-1,布局實現-1

來源:互聯網
上載者:User

流式布局的實現-1,布局實現-1

流式布局可以實現逐行填滿的布局效果;適用於關鍵詞搜尋和熱門展示,可以動態添加標籤,用起來十分方便與快捷

源碼下載(由慕課網的老師提供,謝謝)

之後說說主要的安排:

第一篇:建立類,確定繼承關係,實現建構函式,確定成員函數;

第二篇:實現FlowLayout(流式布局)主要函數的方法;

第一篇:建立類,確定繼承關係,實現建構函式,確定成員函數;

 第二篇與之後幾篇:實現各函數,並說明成員變數的作用;

和用listView實現下拉重新整理一樣,還是先分析檔案結構:

包括了兩個類:

public class MainActivity extends Activity;public class FlowLayout extends ViewGroup;

MainActivity:用於載入主布局,將子View動態添加到自訂布局中;

FlowLayout:繼承自ViewGroup,用於實現流式布局;ViewGroup是容納各種UI組件的容器,繼承自View(官方解釋:A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers.),常見的繼承自ViewGroup的有我們所熟悉的ListView(ListView <- AbsListView <- AdapterView <- ViewGroup);

在Activity中:

成員變數:

private String[] mVals;private FlowLayout mFlowLayout;

mVals:用於動態載入標籤,作為TextView的Text資料來源;

mFlowLayout:FlowLayout布局;

成員函數:

public void initData()

initData:添加標籤,將mVals中的字元寫入到標籤中,並將標籤載入到FlowLayout布局中;

init方法:

public void initData()    {        LayoutInflater mInflater = LayoutInflater.from(this);        for (int i = 0; i < mVals.length; i++)        {            TextView tv = (TextView) mInflater.inflate(R.layout.tv,                    mFlowLayout, false);            tv.setText(mVals[i]);            mFlowLayout.addView(tv);        }     }

首先調用LayoutInflater的靜態成員函數from,獲得主布局的LayoutInflater,以便之後設定主布局中嵌套的FlowLayout布局的View對象;

之後在for迴圈中,用主布局的inflate函數得到一個新的TextView對象,將資料來源mVals中的資料寫入到TextView中,調用FlowLayout布局的addView方法,將新的TextView加入到其中;

在FlowLayout類中:

成員變數:

private List<List<View>> mAllViews;private List<Integer> mLineHeight;

mAllViews:儲存所有的View;

mLineHeight:每一行的高度;

成員函數:

public FlowLayout(Context context);public FlowLayout(Context context, AttributeSet attrs);public FlowLayout(Context context, AttributeSet attrs, int defStyle);protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec);protected void onLayout(boolean changed, int l, int t, int r, int b);public LayoutParams generateLayoutParams(AttributeSet attrs);

各個建構函式的參數,調用時機與重寫:
 一個參數的:為上下文;new一個控制項,傳入的是內容物件;重寫,讓他調用兩個參數的構造方法;
 兩個參數的:為上下文和屬性集;是在布局檔案中書寫控制項的屬性時而沒有自訂屬性時調用;重寫,讓他調用三個參數的構造方法;
 三個參數:為上下文,屬性集和defStyle,是在布局檔案中書寫控制項的屬性時而且使用了自訂屬性時調用;
onMerasure:測量view及其內容來確定view的寬度和高度。這個方法在measure(int, int)中被調用,必須被重寫來精確和有效測量view的內容;

onLayout:在view給其孩子設定尺寸和位置時被調用。子view,包括孩子在內,必須重寫onLayout(boolean, int, int, int, int)方法,並且調用各自的layout(int, int, int, int)方法。參數changed表示view有新的尺寸或位置;參數l表示相對於父view的Left位置;參數t表示相對於父view的Top位置;參數r表示相對於父view的Right位置;參數b表示相對於父view的Bottom位置。

generateLayoutParams:指明ViewGroup與LayoutParams的關係;因為要規定間距,所以直接return new MarginLayoutParams(getContext,attrs);








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.