自定义的GridView的使用:
首先创建一个类,来继承GridView
package com.example.zhanhaoxuan.zhanhaoxuan20171211;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
/**
* Created by zhanhaoxuan on 2017/12/11.
*/
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs,intdefStyleAttr) {
super(context, attrs, defStyleAttr);
}
自定义的组件宽高可能会出现问题 所以需要重新计算
@Override
protected voidonMeasure(intwidthMeasureSpec,int heightMeasureSpec) {
int newheight= MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec,newheight);
}
}
然后XML文件中调用这个GridView
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.zhanhaoxuan.zhanhaoxuan20171211.MyGridView
android:numColumns="3"
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.zhanhaoxuan.zhanhaoxuan20171211.MyGridView>
</RelativeLayout>