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

首頁 > 系統 > Android > 正文

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

2019-10-22 18:35:24
字體:
來源:轉載
供稿:網友

在上一篇中及就寫了自定義view中的onMeausre()和onDraw()兩個方法。在這里就用簡單的流式布局來介紹一下onLayout()方法。

在onLayout方法中有四個參數,我畫了一個簡單的圖來分清楚值哪里。

android,流式布局,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,流式布局,onLayout

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 伊通| 富川| 孝义市| 平和县| 武定县| 南木林县| 丹阳市| 屏东县| 秦安县| 永嘉县| 莆田市| 浦江县| 武定县| 荔波县| 香港| 台北市| 洛阳市| 定陶县| 南城县| 鹤岗市| 兴海县| 山丹县| 武定县| 合肥市| 嘉义县| 永和县| 昌乐县| 清原| 调兵山市| 台东市| 临高县| 波密县| 手游| 武安市| 滦南县| 郧西县| 普洱| 万载县| 当阳市| 兴海县| 东宁县|