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