android流式布局onLayout()方法详解
程序员文章站
2023-12-09 20:58:33
在上一篇中及就写了自定义view中的onmeausre()和ondraw()两个方法。在这里就用简单的流式布局来介绍一下onlayout()方法。
在onlayout...
在上一篇中及就写了自定义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垂直滚动控件ScrollView使用方法详解
-
android流式布局onLayout()方法详解
-
Android编程中光线传感器的调用方法详解
-
Android进度条控件progressbar使用方法详解
-
Android 重写ViewGroup 分析onMeasure()和onLayout()方法
-
Android ExpandableRecyclerView使用方法详解
-
Android编程实现全局获取Context及使用Intent传递对象的方法详解
-
Android中快速便捷的实现圆角按钮方法详解
-
Android编程实现XML解析与保存的三种方法详解
-
Android编程实现禁止状态栏下拉的方法详解