阅读《Android 从入门到精通》(22)——网格视图
Posted SweetLoverFT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阅读《Android 从入门到精通》(22)——网格视图相关的知识,希望对你有一定的参考价值。
网格视图(GridView)
继承于 AdapterView 类,通常需要和列表适配器 ListAdapter 配合使用
GridView 类方法
GridView 示例
完整工程:http://download.csdn.net/detail/sweetloveft/9426612
下述工程主要学习 GridView 的用法,GridView 的运用包含:照片墙、网格布局等。
1.MainActivity.java
package com.sweetlover.activity;
import java.util.ArrayList;
import java.util.List;
import com.sweetlover.gridview.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
public class MainActivity extends Activity
class SelfAdapter extends BaseAdapter
private class Grider
TextView textView;
private Context context = null;
private List<String> lstGrid = null;
private LayoutInflater layoutInflater = null;
public SelfAdapter(Context context)
super();
this.context = context;
public void setList(List<String> lstGrid)
this.lstGrid = lstGrid;
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount()
// TODO Auto-generated method stub
return lstGrid.size();
@Override
public Object getItem(int position)
// TODO Auto-generated method stub
return lstGrid.get(position);
@Override
public long getItemId(int position)
// TODO Auto-generated method stub
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
// TODO Auto-generated method stub
Grider gridHolder = null;
if (convertView != null)
gridHolder = (Grider) convertView.getTag();
else
gridHolder = new Grider();
convertView = layoutInflater.inflate(R.layout.grid, null);
gridHolder.textView = (TextView) convertView
.findViewById(R.id.textView);
convertView.setTag(gridHolder);
gridHolder.textView.setText(lstGrid.get(position));
return convertView;
private GridView gridView = null;
private List<String> lstGrid = null;
private SelfAdapter gridAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i;
gridView = (GridView)findViewById(R.id.gridView);
lstGrid = new ArrayList<String>();
gridAdapter = new SelfAdapter(this);
for (i = 1; i <= 15; i++)
lstGrid.add("Grid_" + i);
gridAdapter.setList(lstGrid);
gridView.setAdapter(gridAdapter);
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
android:columnWidth="90dp"
android:numColumns="auto_fit" >
</GridView>
3.grid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:contentDescription="@string/content_description"
android:src="@drawable/icon" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/imageView" />
</RelativeLayout>
4.AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sweetlover.gridview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.sweetlover.activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
以上是关于阅读《Android 从入门到精通》(22)——网格视图的主要内容,如果未能解决你的问题,请参考以下文章