自动排列android,Android 根据item大小实现自动排列的GridLayout

源码

1. res/values/attrs.xml

2. res/drawable-hdpi/bugdroid.png

3. res/layout/main.xml

xmlns:app="http://schemas.android.com/apk/res/com.example.android.fixedgridlayout"

android:id="@+id/grid"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:cellHeight="100dp"

app:cellWidth="80dp" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/bugdroid" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/bugdroid" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/bugdroid" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/bugdroid" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/bugdroid" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/bugdroid" />

4. src/com/example/android/fixedgridlayout/FixedGridLayout.java

package com.example.android.fixedgridlayout;

import android.content.Context;

import android.content.res.TypedArray;

import android.util.AttributeSet;

import android.view.View;

import android.view.ViewGroup;

/**

* A layout that arranges its children in a grid. The size of the cells is set

* by the {@link #setCellSize} method and the android:cell_width and

* android:cell_height attributes in XML. The number of rows and columns is

* determined at runtime. Each cell contains exactly one view, and they flow in

* the natural child order (the order in which they were added, or the index in

* {@link #addViewAt}. Views can not span multiple cells.

*/

public class FixedGridLayout extends ViewGroup {

int mCellWidth;

int mCellHeight;

public FixedGridLayout(Context context) {

super(context);

}

public FixedGridLayout(Context context, AttributeSet attrs) {

super(context, attrs);

// Read the resource attributes.

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FixedGridLayout);

mCellWidth = a.getDimensionPixelSize(R.styleable.FixedGridLayout_cellWidth, -1);

mCellHeight = a.getDimensionPixelSize(R.styleable.FixedGridLayout_cellHeight, -1);

a.recycle();

}

public void setCellWidth(int px) {

mCellWidth = px;

requestLayout();

}

public void setCellHeight(int px) {

mCellHeight = px;

requestLayout();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int cellWidthSpec = MeasureSpec.makeMeasureSpec(mCellWidth, MeasureSpec.AT_MOST);

int cellHeightSpec = MeasureSpec.makeMeasureSpec(mCellHeight, MeasureSpec.AT_MOST);

int count = getChildCount();

for (int index = 0; index < count; index++) {

final View child = getChildAt(index);

child.measure(cellWidthSpec, cellHeightSpec);

}

// Use the size our parents gave us

setMeasuredDimension(resolveSize(mCellWidth * count, widthMeasureSpec),

resolveSize(mCellHeight * count, heightMeasureSpec));

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

int cellWidth = mCellWidth;

int cellHeight = mCellHeight;

int columns = (r - l) / cellWidth;

if (columns < 0) {

columns = 1;

}

int x = 0;

int y = 0;

int i = 0;

int count = getChildCount();

for (int index = 0; index < count; index++) {

final View child = getChildAt(index);

int w = child.getMeasuredWidth();

int h = child.getMeasuredHeight();

int left = x + ((cellWidth - w) / 2);

int top = y + ((cellHeight - h) / 2);

child.layout(left, top, left + w, top + h);

if (i >= (columns - 1)) {

// advance to next row

i = 0;

x = 0;

y += cellHeight;

} else {

i++;

x += cellWidth;

}

}

}

}

5. com.example.android.fixedgridlayout.FixedGridLayoutTest.java

package com.example.android.fixedgridlayout;

import android.app.Activity;

import android.os.Bundle;

public class FixedGridLayoutTest extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

FixedGridLayout grid = (FixedGridLayout) findViewById(R.id.grid);

grid.setCellWidth(150);

grid.setCellHeight(180);

}

}

说明

FixedGridLayoutTest类,设置如下大小:

grid.setCellWidth(150);

grid.setCellHeight(180);

效果图:

0818b9ca8b590ca3270a3433284dd417.png

FixedGridLayoutTest类,设置如下大小:

grid.setCellWidth(280);

grid.setCellHeight(180);

效果图:

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ava实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),可运行高分资源 Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值