【Android】 LinearLayout 自動換行

以下的圖片來自KKbox的截圖,這篇主要講解如何實作出熱門關鍵字的View,上網搜尋後發現,還蠻多app都有採用這種View的,像是淘寶、天貓


主要將View繼承ViewGroup,接著override onMeasureonLayout兩個方法。
這邊比較特別的是還有override generateLayoutParams method,主要是為了取得margin的。
而在onMeasure中,其實有很多種做法,來取得所要的高度,像是MeasureSpec.UNSPECIFIED等方法的搭配使用,等熟悉點,或許能用此種方式。
其他有關method的解釋都寫在註解中了。

附上完整程式碼【github】LinearLayout 自動換行

)

    Written by

    Android Developer @ Travelstart

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade