Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自訂 ViewGroup 起步:實作最簡單的橫向排列版面(1)

編輯:關於 Android 程式設計

最近在學自訂 ViewGroup,最終目標很明確:做一個類似可以滾動的 ListView,用 Adapter 填充資料,並且支援 notifyDataSetChanged() 更新。

不過一口吃不成胖子(我這身材可是好幾年累積的成果~~~~),還是一步一步來。這篇筆記先寫最基礎的橫向排列。

程式碼:

 package com.example.libingyuan.horizontallistview.ScrollViewGroup; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; /** * 自訂ViewGroup * 很簡單的橫向布局,把所有的子View都橫著排列起來,不可滾動 */ public class ScrollViewGroup extends ViewGroup{ public ScrollViewGroup(Context context) { this(context,null); } public ScrollViewGroup(Context context, AttributeSet attrs) { this(context, attrs,0); } public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //重新設置寬高 this.setMeasuredDimension(measureWidth(widthMeasureSpec,heightMeasureSpec),measureHeight(widthMeasureSpec,heightMeasureSpec)); } /** * 測量寬度 */ private int measureWidth(int widthMeasureSpec, int heightMeasureSpec) { // 寬度 int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); //寬度的類型 int modeWidth = MeasureSpec.getMode(widthMeasureSpec); //父控件的寬(wrap_content) int width = 0; //子View的個數 int childCount = getChildCount(); //重新測量子view的寬度,以及最大高度 for (int i = 0; i < childCount; i++) { //取得子View View child = getChildAt(i); //測量子View,無論什麼模式,這句必須有否則介面不顯示子View(一片空白) measureChild(child, widthMeasureSpec, heightMeasureSpec); //得到子View的邊距 MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); //得到寬度 int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; //寬度累加 width += childWidth; } //返回寬度 return modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width; } /** * 測量高度 */ private int measureHeight(int widthMeasureSpec, int heightMeasureSpec) { //高度 int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); //高度的模式 int modeHeight = MeasureSpec.getMode(heightMeasureSpec); //父控件的高(wrap_content) int height = 0; //子View的個數 int childCount = getChildCount(); //重新測量子view的寬度,以及最大高度 for (int i = 0; i < childCount; i++) { //得到子View View child = getChildAt(i); //測量 measureChild(child, widthMeasureSpec, heightMeasureSpec); //得到邊距 MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); //得到高度 int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; //累加高度 height += childHeight; } //求平均高度 height = height / childCount; //返回高度 return modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height; } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childLeft=0;//子View左邊的距離 int childWidth;//子View的寬度 int height=getHeight(); int childCount=getChildCount(); for (int i = 0; i < childCount; i++) { View child=getChildAt(i); MarginLayoutParams lp= (MarginLayoutParams) child.getLayoutParams(); childWidth=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin; //最主要的一句話 child.layout(childLeft,0,childLeft+childWidth,height); childLeft+=childWidth; } } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(),attrs); } } 

三個關鍵環節

第一是 onMeasure 中一定要呼叫 measureChild,這句話漏掉,子 View 就不會被測量,畫面直接一片空白——這是新手最常踩的坑。

第二是寬度累加時要把 MarginLayoutParams 的 leftMargin 與 rightMargin 一起算進去,而 generateLayoutParams 回傳 MarginLayoutParams 正是為了讓 margin 屬性生效。

第三是高度取平均(height / childCount),這是個簡化處理,橫向列表裡各項高度通常一致,但正式專案中若允許不等高,應改為取最大值。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved