国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-12-12 02:44:00
字體:
來源:轉載
供稿:網友

在上一篇中及就寫了自定義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>

效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 仲巴县| 旺苍县| 鸡泽县| 黄梅县| 旬阳县| 金阳县| 岑溪市| 元谋县| 延津县| 沙雅县| 斗六市| 化隆| 长宁区| 建德市| 寿阳县| 普定县| 宁阳县| 康保县| 德保县| 和顺县| 江源县| 南川市| 泽州县| 当涂县| 绥宁县| 绵竹市| 五莲县| 婺源县| 临湘市| 通化县| 万全县| 东台市| 阳山县| 大田县| 同心县| 通城县| 宁波市| 庄河市| 泰安市| 汤原县| 莱西市|