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

Android編程重寫ViewGroup實(shí)現(xiàn)卡片布局的方法

 更新時(shí)間:2016年02月17日 09:41:18   作者:xurong  
這篇文章主要介紹了Android編程重寫ViewGroup實(shí)現(xiàn)卡片布局的方法,實(shí)例分析新建FlowLayout繼承ViewGroup類及設(shè)置布局文件實(shí)現(xiàn)卡片布局效果的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程重寫ViewGroup實(shí)現(xiàn)卡片布局的方法。分享給大家供大家參考,具體如下:

實(shí)現(xiàn)效果如圖

實(shí)現(xiàn)思路

1. 重寫onMeasure(int widthMeasureSpec, int heightMeasureSpec)設(shè)置每個(gè)子View的大小

2. 重寫onLayout(boolean changed, int l, int t, int r, int b) 設(shè)置每個(gè)子View的位置

第一步:新建FlowLayout繼承ViewGroup

package com.rong.activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
 * 卡片布局
 * 
 * @author 徐榮
 *
 */
public class FlowLayout extends ViewGroup {
  public FlowLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // 當(dāng)前子View的數(shù)量
    int childSize = getChildCount();
    // 獲取行寬
    int lineWidth = getMeasuredWidth();
    // 當(dāng)前是第幾行
    int lines = 1;
    // 當(dāng)前累加的行寬
    int nowLineWidth = 0;
    for (int i = 0; i < childSize; i++) {
      View view = getChildAt(i);
      // 子View的寬度
      int childWidth = view.getMeasuredWidth();
      // 子View的高度
      int childHeight = view.getMeasuredHeight();
      // 如果當(dāng)前的nowLineWidth+childWidth>= lineWidth 則換行
      if (nowLineWidth + childWidth >= lineWidth) {
        nowLineWidth = 0;
        lines = lines + 1;
      }
      // 設(shè)置子View的位置
      view.layout(nowLineWidth, childHeight * (lines - 1), nowLineWidth + childWidth, childHeight * lines);
      nowLineWidth = nowLineWidth + childWidth;
      // 如果nowLineWidth >= lineWidth 則換行
      if (nowLineWidth >= lineWidth) {
        nowLineWidth = 0;
        lines = lines + 1;
      }
    }
  }
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    // 設(shè)置自己View的大小
    setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    for (int i = 0; i < getChildCount(); i++) {
      View view = getChildAt(i);
      // 設(shè)置每個(gè)子View的大小
      view.measure(view.getMeasuredWidth(), view.getMeasuredHeight());
    }
  }
}

第二步:新建布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@android:color/black"
  android:orientation="vertical" >
  <com.rong.activity.FlowLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" >
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Apple" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Button" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Cup" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Double" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Ear" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Flower" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Game" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hotdog" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="interseting" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="joker" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="king" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="mother" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="lost" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="noting" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="orange" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="poker" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="qustion" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="ring" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="string" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="type" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="unit" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="vertion" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="west" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="x" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="young" />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="zip" />
  </com.rong.activity.FlowLayout>
</RelativeLayout>

運(yùn)行!

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

姜堰市| 广河县| 治县。| 漯河市| 铁岭县| 游戏| 灌阳县| 连云港市| 通州区| 咸丰县| 博乐市| 五常市| 个旧市| 长治市| 诏安县| 新和县| 肥东县| 鹤庆县| 读书| 怀远县| 禹城市| 德格县| 天水市| 宁德市| 盐边县| 兴文县| 遵义市| 宁南县| 凉山| 玉树县| 温宿县| 德令哈市| 宜城市| 平舆县| 天等县| 班戈县| 固原市| 安龙县| 桂平市| 沈丘县| 安义县|