首先是布局
activity_main.xml
gridview的padding可以通过在外边嵌套一层布局添加 paddinng,例如:LinearLayout。
gridview的父控件也必须用match_parrent, gridview必须用 match_parent,才能获得它的实际宽高
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#999999"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/linearLayout"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="25dp"
android:paddingBottom="25dp"
>
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#666666"
android:numColumns="4"
android:scrollbars="none" >
</GridView>
</LinearLayout>
</RelativeLayout>
griditem_app.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
2 GridViewAdapter.java
package com.example.gridviewtest;
import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class GridViewAdapter extends BaseAdapter {
private int itemWidth = 0;
private int count = 3;
@Override
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public void setItemHeight(int itemWidth) {
this.itemWidth = itemWidth;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.griditem_app, null);
}
//从外部传进来计算好的item 高度进行设置
convertView.setMinimumHeight(itemWidth);
return convertView;
}
}
MainActivity.java
package com.example.gridviewtest;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.GridView;
public class MainActivity extends Activity {
private GridView gridview;
private GridViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.gridView1);
//获得手机密度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final float denScale = (float) dm.densityDpi / (float) DisplayMetrics.DENSITY_DEFAULT;
adapter = new GridViewAdapter();
//在主线程绘图渲染的时候计算gridview宽高
gridview.post(new Runnable() {
@Override
public void run() {
int width = gridview.getMeasuredWidth();
int height = gridview.getMeasuredHeight();
int itemWidth = (int) (denScale * 80);
//计算一行能容纳的item个数
int count = width / itemWidth;
//设置一行能放下的item个数
gridview.setNumColumns(count);
//计算gridview可见高度所能容纳的item行数
int rowCount = height / itemWidth;
//设置item高度
adapter.setItemHeight((int) ((float) height / (float) rowCount));
adapter.setCount(count * rowCount-9);
gridview.setAdapter(adapter);
}
});
}
}
效果图