android 矩形布局
来源:互联网 发布:单点登录系统源码 编辑:程序博客网 时间:2024/06/17 00:42
在开发中可能你需要一个2*2,或者3*3,N*N的布局,但是又不会涉及到滑动,此时可以自定义一个不需要滑动的网格布局。
public class UserTopicGridView extends RelativeLayout{
private Context mContext;
private Activity mActivity;
private String title;
private TApplication mTApplication;
private int columnLine = 2;
private int childWidth = 100;
private int childHeight = 100;
private int horSpace = 10;
private int verSpace = 10;
public UserTopicGridView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
mActivity = (Activity) this.mContext;
mTApplication = (TApplication) mActivity.getApplication();
}
public void setTitle(String title) { this.title = title;}public void setChildSize(int width ,int height) { this.childWidth = width; this.childHeight = height; requestLayout();}public void setPadding(int horSpace, int verSpace) { this.horSpace = horSpace; this.verSpace = verSpace; requestLayout();}public void initViews(final HomeListBean homeListBean) { List <Object> list = homeListBean.items; for(int i = 0; i < list.size(); i++) { ImageView imageView = new ImageView(mContext); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(childWidth, childHeight); imageView.setLayoutParams(params); imageView.setScaleType(ScaleType.FIT_XY); ImageLoader.getInstance().displayImage((String)list.get(i), imageView, mTApplication.getOptions()); imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //do something } }); addView(imageView); }}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int count = getChildCount(); for (int i = 0; i < count; i++) { int childWidthMeasureSpec = MeasureSpec .makeMeasureSpec(childWidth, MeasureSpec.EXACTLY); int childheightMeasureSpec = MeasureSpec .makeMeasureSpec(childHeight, MeasureSpec.EXACTLY); View child = getChildAt(i); child.measure(childWidthMeasureSpec, childheightMeasureSpec); } int newParentWidth = childWidth * columnLine + horSpace * (columnLine - 1); int newParentHeight = childHeight * getLine() + verSpace * (getLine() - 1); setMeasuredDimension(newParentWidth, newParentHeight);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = getChildCount(); int left = 0; int top = 0; for(int i = 0; i < childCount; i++) { if(isNewLine(i)) { top += childHeight + verSpace; left = 0; } View view = getChildAt(i); view.layout(left, top, left + childWidth, childHeight + top); left += childWidth + horSpace; }}private boolean isNewLine(int index) { if(index !=0 && index % columnLine == 0) { return true; } return false;}private int getLine() { int line = 0; line = getChildCount() / 2; if(getChildCount() % columnLine != 0) { line += 1; } return line;}
}
第一篇博客,略显粗糙,没有注释,但是相信大家都会懂,晚安。
0 0
- android 矩形布局
- Android 圆角矩形
- android 矩形碰撞
- Android shape 矩形边框
- Android画带圆边的矩形
- 手机端H5页面-矩形布局
- Android 布局:相对布局
- Android画矩形小程序
- Android实现矩形设置菜单
- android 矩形变换 图像旋转
- Android绘制矩形参数说明
- android圆角矩形背景
- android圆角矩形进度条
- Android绘制空心矩形示例
- Android 圆角矩形ImageView
- Android自定义圆角矩形
- Android之圆角矩形
- Android 半圆矩形的实现
- hdu 5340 Manachers + 枚举
- H5+写文件
- leetcode——Count Primes
- nginx支持ssl简介
- 应聘中高级工程师准备
- android 矩形布局
- 比特币钱包应用breadwallet源码iOS版
- 博弈知识汇总
- 使用Spring的jdbcTemplate进一步简化JDBC操作
- mmap 函数
- AngularJs 初学
- linux kafka 搭建运行环境
- .net连接sqlite数据库
- Xcode 报错: Extra argument in call