Android 自定义类似win 8样式的GridView
我需要自定义一个gridview,可以显示如下图片 正如您所看到的,每个补丁的宽度和高度都是相同的。我自定义了gridview,但遇到了一个问题。当滚动屏幕时,会多次调用onMeasure()方法。即使您停止滚动屏幕,也会调用它Android 自定义类似win 8样式的GridView,android,gridview,viewgroup,Android,Gridview,Viewgroup,我需要自定义一个gridview,可以显示如下图片 正如您所看到的,每个补丁的宽度和高度都是相同的。我自定义了gridview,但遇到了一个问题。当滚动屏幕时,会多次调用onMeasure()方法。即使您停止滚动屏幕,也会调用它 public class FixRowGridView extends GridView { private static final String TAG = "FixRowGridView"; private int mChildHeight; public
public class FixRowGridView extends GridView {
private static final String TAG = "FixRowGridView";
private int mChildHeight;
public FixRowGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public FixRowGridView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public FixRowGridView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
mChildHeight = (height - 3 * DimensionAdjustUtil.dipToPix(getContext(), 10)) / 4;
int width = (mChildHeight << 1) + DimensionAdjustUtil.dipToPix(getContext(), 10);
setMeasuredDimension(width, height);
DumpUtil.dLog(TAG, "The height is " + height);
/*int width = (mChildHeight << 1) + DimensionAdjustUtil.dipToPix(getContext(), 10);
setMeasuredDimension(width, height);*/
/*setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));*/
}
@Override
protected void layoutChildren() {
super.layoutChildren();
int count = getChildCount();
for(int i = 0; i < count; i++) {
View view = getChildAt(i);
LayoutParams params = (LayoutParams) view.getLayoutParams();
params.height = mChildHeight;
params.width = mChildHeight;
view.setLayoutParams(params);
}
}
公共类FixRowGridView扩展了GridView{
私有静态最终字符串标记=“FixRowGridView”;
私人麦希尔德赫特酒店;
公共FixRowGridView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
//TODO自动生成的构造函数存根
}
公共FixRowGridView(上下文、属性集属性){
超级(上下文,attrs);
//TODO自动生成的构造函数存根
}
公共FixRowGridView(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
//TODO自动生成的方法存根
超级测量(宽度测量、高度测量);
int height=MeasureSpec.getSize(heightMeasureSpec);
mChildHeight=(高度-3*DimensionAdjustUtil.dipToPix(getContext(),10))/4;
int width=(mChildHeight问题已经解决,方法如下:
1.根据手机尺寸计算合适的高度和宽度。
2.设置从步骤1到项目的固定值
希望它能帮助你