关于gridview水平居中,平均布局

首先是布局

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);
			}
		});
	}
}


效果图








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值