Android編程重寫ViewGroup實現(xiàn)卡片布局的方法
本文實例講述了Android編程重寫ViewGroup實現(xiàn)卡片布局的方法。分享給大家供大家參考,具體如下:
實現(xiàn)效果如圖:
實現(xiàn)思路
1. 重寫onMeasure(int widthMeasureSpec, int heightMeasureSpec)設(shè)置每個子View的大小
2. 重寫onLayout(boolean changed, int l, int t, int r, int b) 設(shè)置每個子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的數(shù)量 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; } // 設(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è)置每個子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>
運行!
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- android Matrix實現(xiàn)圖片隨意放大縮小或拖動
- Android實現(xiàn)ImageView圖片縮放和拖動
- Android編程實現(xiàn)圖片的瀏覽、縮放、拖動和自動居中效果
- Android實現(xiàn)圖片拖動效果
- Android如何創(chuàng)建可拖動的圖片控件
- Android通過自定義ImageView控件實現(xiàn)圖片的縮放和拖動的實現(xiàn)代碼
- Android RecyclerView多類型布局卡片解決方案
- Android實現(xiàn)簡單卡片布局
- Android控件CardView實現(xiàn)卡片布局
- Android實現(xiàn)可拖動層疊卡片布局
相關(guān)文章
Android實現(xiàn)3種側(cè)滑效果(仿qq側(cè)滑、抽屜側(cè)滑、普通側(cè)滑)
這篇文章主要為大家詳細介紹了Android實現(xiàn)多種側(cè)滑效果,包括仿qq側(cè)滑,抽屜側(cè)滑,普通側(cè)滑三種效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04Android開發(fā)中使用WebView控件瀏覽網(wǎng)頁的方法詳解
這篇文章主要介紹了Android開發(fā)中使用WebView控件瀏覽網(wǎng)頁的方法,結(jié)合實例形式較為詳細的總結(jié)分析了Android WebView控件的功能、布局、設(shè)置、常用方法及相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android自定義View 仿QQ側(cè)滑菜單的實現(xiàn)代碼
這篇文章主要介紹了Android自定義View 仿QQ側(cè)滑菜單的實現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-08-08Android 實現(xiàn)左滑出現(xiàn)刪除選項
滑動刪除的部分主要包含兩個部分, 一個是內(nèi)容區(qū)域(用于放置正常顯示的view),另一個是操作區(qū)域(用于放置刪除按鈕)。下面通過本文給大家介紹Android 實現(xiàn)左滑出現(xiàn)刪除選項,需要的朋友可以參考下2017-06-06