Android Layout学习 自己layout 布局中的item

来源:互联网 发布:java 百战天虫 编辑:程序博客网 时间:2024/04/29 06:52
package com.example.testwin8grid;import java.util.ArrayList;import java.util.List;import android.R.integer;import android.content.Context;import android.graphics.Color;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.view.View.MeasureSpec;import android.widget.GridLayout;import android.widget.GridView;import android.widget.Space;public class CellHome extends ViewGroup {private List<CellView> cellViews = new ArrayList<CellView>();public static int colums = 10;// 横向有多少列多少行public int itemlength = 100;private int mHorizontalSpacing = 30;// 设置默认的 水平间距private int mVerticalSpacing = 15; // 设置默认的竖直间距public CellHome(Context context) {super(context);this.setBackgroundColor(Color.WHITE);}public void addView(CellView cellView) {cellViews.add(cellView);addView(cellView.getView());}public void addView(View view) {super.addView(view);}/** * 先要知道他的colum 才能知道他的高度 要想知道他的高度 就要 做layout 知道他的 */@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {Log.i("onMeasure", "=======");int width = MeasureSpec.getSize(widthMeasureSpec);// 每次初始化 最多显示10行colums = 10;// 根据屏幕判断 最多显示多少行while ((width < colums * itemlength + (colums + 1) * mHorizontalSpacing)) {colums--;}int childrenCount = getChildCount();int lines = childrenCount / colums + 1;// 确定高度是多少 如果不设置 在他上面套surfaceview将会出现 社么也不显示int height = lines * itemlength + (lines + 1) * mVerticalSpacing;super.onMeasure(width, height);setMeasuredDimension(width, height);}protected void onLayout(boolean changed, int l, int t, int r, int b) {Log.i("onLayout", "=======");// onMeasure(100, 100);int width = getWidth();// 计算真正的xspcae.int xSpace = (width - itemlength * colums) / (colums + 1); // 算出水平空白的宽度if (getChildCount() < colums) {// 如果child数量太少,则 这个宽度不能大于默认宽度xSpace = xSpace > mHorizontalSpacing ? mHorizontalSpacing : xSpace;}// / 先假想可以显示这么多row个// int row = height / itemlength ;// int ySpace = (height - itemlength * row) / (row + 1);// 使用默认的高度int ySpace = mVerticalSpacing;for (int i = 0; i < getChildCount(); i++) {CellView cellView = cellViews.get(i);int childL, childT, childR, childB;childL = xSpace + ((cellView.index) % colums)* (xSpace + itemlength);childT = ySpace + (cellView.index / colums) * (ySpace + itemlength);// 计算那个view所在在位置childR = childL + itemlength;childB = childT + itemlength;getChildAt(i).layout(childL, childT, childR, childB);}}}

package com.example.testwin8grid;import android.view.View;public class CellView {View view;int index;int vgx;// child在vg中的x位置 根据index计算int vgy; // child在vg中的y位置// vgx = index %3 vgy = index /3 index = vgx+vgy*3public CellView(View view, int index) {this.index = index;this.view = view;}public View getView() {return view;}public void setView(View view) {this.view = view;}public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}public int getVgx() {return vgx;}public void setVgx(int vgx) {this.vgx = vgx;}public int getVgy() {return vgy;}public void setVgy(int vgy) {this.vgy = vgy;}}
CellHome cellHome = new CellHome(this);cellHome.setBackgroundColor(Color.LTGRAY);for (int i = 0; i < 20; i++) {TextView view = new TextView(this);view.setBackgroundColor(Color.GRAY);view.setText("" + i);CellView cellView = new CellView(view, i);cellHome.addView(cellView);}ScrollView scrollView = new ScrollView(this);scrollView.setPadding(10, 10, 10, 10);scrollView.addView(cellHome);setContentView(scrollView);