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

首頁 > 系統(tǒng) > Android > 正文

Android編程重寫ViewGroup實現卡片布局的方法

2020-04-11 10:48:37
字體:
來源:轉載
供稿:網友

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

實現效果如圖

實現思路

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

2. 重寫onLayout(boolean changed, int l, int t, int r, int b) 設置每個子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) {    // 當前子View的數量    int childSize = getChildCount();    // 獲取行寬    int lineWidth = getMeasuredWidth();    // 當前是第幾行    int lines = 1;    // 當前累加的行寬    int nowLineWidth = 0;    for (int i = 0; i < childSize; i++) {      View view = getChildAt(i);      // 子View的寬度      int childWidth = view.getMeasuredWidth();      // 子View的高度      int childHeight = view.getMeasuredHeight();      // 如果當前的nowLineWidth+childWidth>= lineWidth 則換行      if (nowLineWidth + childWidth >= lineWidth) {        nowLineWidth = 0;        lines = lines + 1;      }      // 設置子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);    // 設置自己View的大小    setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);    for (int i = 0; i < getChildCount(); i++) {      View view = getChildAt(i);      // 設置每個子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>

運行!

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

希望本文所述對大家Android程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 广丰县| 林西县| 伊川县| 新郑市| 自贡市| 桃源县| 东城区| 平邑县| 确山县| 阿坝县| 堆龙德庆县| 南平市| 平江县| 惠安县| 涟水县| 溧阳市| 江城| 腾冲县| 民乐县| 介休市| 南开区| 诏安县| 长武县| 湟中县| 宜章县| 宁乡县| 曲松县| 塔河县| 广东省| 牡丹江市| 阳东县| 新津县| 云阳县| 大理市| 凭祥市| 夏津县| 夏津县| 永康市| 武隆县| 枞阳县| 井研县|