android流式布局onLayout()方法詳解
在上一篇中及就寫(xiě)了自定義view中的onMeausre()和onDraw()兩個(gè)方法。在這里就用簡(jiǎn)單的流式布局來(lái)介紹一下onLayout()方法。
在onLayout方法中有四個(gè)參數(shù),我畫(huà)了一個(gè)簡(jiǎn)單的圖來(lái)分清楚值哪里。
好啦,現(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);
//測(cè)量所有子控件
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>
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android流式布局FlowLayout詳解
- Android流式布局實(shí)現(xiàn)歷史搜索記錄功能
- Android實(shí)現(xiàn)熱門(mén)標(biāo)簽的流式布局
- Java Swing組件布局管理器之FlowLayout(流式布局)入門(mén)教程
- Android簡(jiǎn)單實(shí)現(xiàn)自定義流式布局的方法
- Android自定義ViewGroup之實(shí)現(xiàn)FlowLayout流式布局
- Android 簡(jiǎn)單實(shí)現(xiàn)一個(gè)流式布局的示例
- Android自定義流式布局/自動(dòng)換行布局實(shí)例
- python GUI框架pyqt5 對(duì)圖片進(jìn)行流式布局的方法(瀑布流flowlayout)
- Flexbox+ReclyclerView實(shí)現(xiàn)流式布局
相關(guān)文章
Android設(shè)置個(gè)性化Dialog小圖標(biāo)的方法
這篇文章主要介紹了Android設(shè)置個(gè)性化Dialog小圖標(biāo)的方法,涉及Android針對(duì)系統(tǒng)資源的設(shè)置與調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android中進(jìn)程生命周期的優(yōu)先級(jí)
這篇文章主要介紹了Android中進(jìn)程生命周期的優(yōu)先級(jí)的相關(guān)資料,需要的朋友可以參考下2016-01-01
詳解Android Activity之間跳轉(zhuǎn)出現(xiàn)短暫黑屏的處理方法
本篇文章主要介紹了詳解Android Activity之間跳轉(zhuǎn)出現(xiàn)短暫黑屏的處理方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-06-06
Android實(shí)現(xiàn)美團(tuán)外賣(mài)底部導(dǎo)航欄動(dòng)畫(huà)
這篇文章主要介紹了Android實(shí)現(xiàn)美團(tuán)外賣(mài)底部導(dǎo)航欄動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android中使用TextView實(shí)現(xiàn)高仿京東淘寶各種倒計(jì)時(shí)效果
今天給大家?guī)?lái)的是僅僅使用一個(gè)TextView實(shí)現(xiàn)一個(gè)高仿京東、淘寶、唯品會(huì)等各種電商APP的活動(dòng)倒計(jì)時(shí)。今天小編把實(shí)現(xiàn)代碼分享到腳本之家平臺(tái),對(duì)android textclock 倒計(jì)時(shí)效果感興趣的朋友參考下吧2016-10-10
詳解Android ViewPager2中的緩存和復(fù)用機(jī)制
最近接觸到豎向整頁(yè)滑動(dòng)的需求,發(fā)現(xiàn)了viewpager2,viewpager2支持fragment,保留了viewpager的特性,下面這篇文章主要給大家介紹了關(guān)于ViewPager2中的緩存和復(fù)用機(jī)制的相關(guān)資料,需要的朋友可以參考下2021-11-11

