最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年06月07日 09:11:56   作者:ITqingliang  
這篇文章主要為大家詳細(xì)介紹了android流式布局的onLayout()方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

 

好啦,現(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;
        //換行后,第一個控件作為最大行高
        lineHeight = ch;
      }else{
        //如果不換行,需要計(jì)算最大高度
        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>

效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

沁阳市| 石屏县| 大英县| 广元市| 陇川县| 车险| 丁青县| 滨州市| 通渭县| 理塘县| 吴川市| 南康市| 古蔺县| 蚌埠市| 页游| 宜兰县| 崇义县| 沁水县| 聂拉木县| 昭苏县| 神农架林区| 虎林市| 军事| 贵定县| 兰西县| 嘉义县| 藁城市| 原阳县| 台南县| 吴江市| 烟台市| 来宾市| 河间市| 隆尧县| 天津市| 东乡县| 色达县| 扶风县| 宾阳县| 漳州市| 高州市|