android流式布局onLayout()方法详解

所属分类: 软件编程 / Android 阅读数: 43
收藏 0 赞 0 分享

在上一篇中及就写了自定义view中的onMeausre()和onDraw()两个方法。在这里就用简单的流式布局来介绍一下onLayout()方法。

在onLayout方法中有四个参数,我画了一个简单的图来分清楚值哪里。

 

好啦,现在就直接看代码吧。

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) {
    //获得子控件的数量
    int childCount = getChildCount();
    //当前子控件的左边坐标
    int cl = 0;
    //当前子控件的上边坐标
    int ct = 0;
    //ViewGroup整体宽度
    int width = r - l;
    //行高
    int lineHeight = 0;
    //遍历所有子控件
    for(int i = 0; i < childCount; i++){
      //获取当前控件
      View childAt = getChildAt(i);
      //获取宽度
      int cw = childAt.getMeasuredWidth();
      //获取高度
      int ch = childAt.getMeasuredHeight();
      //当前控件右边
      int cr = cl + cw;
      //当前控件下边
      int cb = ct + ch;
      //判断是否换行
      if(cr > width){
        //如果换行重新计算上下左右地值
        cl = 0;
        cr = cl + cw;
        ct += lineHeight;
        cb = ct + ch;
        //换行后,第一个控件作为最大行高
        lineHeight = ch;
      }else{
        //如果不换行,需要计算最大高度
        lineHeight = Math.max(lineHeight,ch);
      }
      childAt.layout(cl,ct,cr,cb);
      //横向向后移动一个,前面控件的右边作为后面控件的左边
      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>

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多