android流式布局onLayout()方法詳解
在上一篇中及就寫了自定義view中的onMeausre()和onDraw()兩個(gè)方法。在這里就用簡單的流式布局來介紹一下onLayout()方法。
在onLayout方法中有四個(gè)參數(shù),我畫了一個(gè)簡單的圖來分清楚值哪里。
好啦,現(xiàn)在就直接看代碼吧。
FlowLayout.Java
package com.example.my_view; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; /** * 自定義布局 流布局 */ public class FlowLayout extends ViewGroup { public FlowLayout(Context context) { super(context); } public FlowLayout(Context context, AttributeSet attrs) { super(context, attrs); } /** * * @param changed * @param l 左 * @param t 上 * @param r 右 * @param b 下 */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { //獲得子控件的數(shù)量 int childCount = getChildCount(); //當(dāng)前子控件的左邊坐標(biāo) int cl = 0; //當(dāng)前子控件的上邊坐標(biāo) int ct = 0; //ViewGroup整體寬度 int width = r - l; //行高 int lineHeight = 0; //遍歷所有子控件 for(int i = 0; i < childCount; i++){ //獲取當(dāng)前控件 View childAt = getChildAt(i); //獲取寬度 int cw = childAt.getMeasuredWidth(); //獲取高度 int ch = childAt.getMeasuredHeight(); //當(dāng)前控件右邊 int cr = cl + cw; //當(dāng)前控件下邊 int cb = ct + ch; //判斷是否換行 if(cr > width){ //如果換行重新計(jì)算上下左右地值 cl = 0; cr = cl + cw; ct += lineHeight; cb = ct + ch; //換行后,第一個(gè)控件作為最大行高 lineHeight = ch; }else{ //如果不換行,需要計(jì)算最大高度 lineHeight = Math.max(lineHeight,ch); } childAt.layout(cl,ct,cr,cb); //橫向向后移動(dòng)一個(gè),前面控件的右邊作為后面控件的左邊 cl = cr; } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //測量所有子控件 measureChildren(widthMeasureSpec, heightMeasureSpec); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <com.example.my_view.FlowLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.my_view.MainActivity"> <!-- <com.example.my_view.Counter android:layout_width="match_parent" android:layout_height="wrap_content" app:number="10" app:bgColor="#ff002b" app:textColor="#0fd444"/>--> <!--<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我在自定義布局的下面"/>--> <Button android:layout_width="200dp" android:layout_height="50dp" android:text="button1"/> <Button android:layout_width="100dp" android:layout_height="100dp" android:text="button2"/> <Button android:layout_width="180dp" android:layout_height="60dp" android:text="button3"/> <Button android:layout_width="100dp" android:layout_height="50dp" android:text="button4"/> <Button android:layout_width="80dp" android:layout_height="100dp" android:text="button5"/> <Button android:layout_width="100dp" android:layout_height="50dp" android:text="button6"/> <Button android:layout_width="120dp" android:layout_height="70dp" android:text="button7"/> <Button android:layout_width="100dp" android:layout_height="50dp" android:text="button8"/> </com.example.my_view.FlowLayout>
效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android流式布局FlowLayout詳解
- Android流式布局實(shí)現(xiàn)歷史搜索記錄功能
- Android實(shí)現(xiàn)熱門標(biāo)簽的流式布局
- Java Swing組件布局管理器之FlowLayout(流式布局)入門教程
- Android簡單實(shí)現(xiàn)自定義流式布局的方法
- Android自定義ViewGroup之實(shí)現(xiàn)FlowLayout流式布局
- Android 簡單實(shí)現(xiàn)一個(gè)流式布局的示例
- Android自定義流式布局/自動(dòng)換行布局實(shí)例
- python GUI框架pyqt5 對圖片進(jìn)行流式布局的方法(瀑布流flowlayout)
- Flexbox+ReclyclerView實(shí)現(xiàn)流式布局
相關(guān)文章
Android使用Item Swipemenulistview實(shí)現(xiàn)仿QQ側(cè)滑刪除功能
大家都用過QQ,肯定有人好奇QQ滑動(dòng)刪除Item的效果是怎樣實(shí)現(xiàn)的,其實(shí)我們使用Swipemenulistview就可以簡單的實(shí)現(xiàn)。這篇文章主要介紹了Android使用ItemSwipemenulistview實(shí)現(xiàn)仿QQ側(cè)滑刪除功能,需要的朋友可以參考下2017-02-02Android RecyclerView實(shí)現(xiàn)多種item布局的方法
本篇文章主要介紹了Android RecyclerView實(shí)現(xiàn)多種item布局的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能的完整代碼
這篇文章主要介紹了Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android Studio 中aidl的自定義類的使用詳解
這篇文章主要介紹了Android Studio 中aidl的自定義類的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android實(shí)現(xiàn)布局動(dòng)畫和共享動(dòng)畫的結(jié)合效果
今天給大家?guī)砟軌蛱嵘脩趔w驗(yàn)感的交互動(dòng)畫,使用起來非常簡單,體驗(yàn)效果非常贊,其中僅使用到布局動(dòng)畫和共享動(dòng)畫,文章通過代碼示例介紹的非常詳細(xì),感興趣的同學(xué)可以自己動(dòng)手試一試2023-09-09android studio 3.0 gradle 打包腳本配置詳解
這篇文章主要介紹了android studio 3.0 gradle 打包腳本配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Android自定義動(dòng)態(tài)壁紙開發(fā)詳解
這篇文章主要為大家詳細(xì)介紹了Android自定義動(dòng)態(tài)壁紙開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android利用FlexboxLayout輕松實(shí)現(xiàn)流動(dòng)布局
flexbox是屬于CSS的一種布局方案,可以簡單、完整、響應(yīng)式的實(shí)現(xiàn)各種頁面布局。谷歌將其引入以提高復(fù)雜布局的能力。下面這篇文章主要給大家介紹了在Android中利用FlexboxLayout輕松實(shí)現(xiàn)流動(dòng)布局的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04Android組件ContextMenu實(shí)現(xiàn)長按事件
這篇文章主要為大家詳細(xì)介紹了Android組件ContextMenu實(shí)現(xiàn)長按事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04Android自定義View之邊框文字、閃爍發(fā)光文字
這篇文章主要為大家詳細(xì)介紹了Android自定義View之邊框文字、閃爍發(fā)光文字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01