GridView

下面介绍一下GridView,

GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示图片,图片等内容,比如实现九宫格图,用GridView是首选,也是最简单的。主要用于设置Adapter。
GridView常用的XML属性:
 

属性名称

描述

android:columnWidth

设置列的宽度。

android:gravity

设置此组件中的内容在组件中的位置。可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical可以多选,用“|”分开。

android:horizontalSpacing

两列之间的间距。

android:numColumns

设置列数。

android:stretchMode

缩放模式。

android:verticalSpacing

两行之间的间距。

下面是一个小程序:MainActivity.java代码如下:

package com.example.gridview;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class MainActivity extends Activity {

int[] drawableIds={R.drawable.andy,R.drawable.bill,R.drawable.torvalds,R.drawable.turing};
int[] nameIds={R.string.andy,R.string.bill,R.string.torvalds,R.string.turing};
int[] msgIds={R.string.andydis,R.string.billdis,R.string.torvaldsdis,R.string.turingdis};
public List<? extends Map<String, ?>> generateDataList(){
ArrayList<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
int rowCounter=drawableIds.length;
for(int i=0;i<rowCounter;i++)
{
HashMap<String, Object> hashMap=new HashMap<String, Object>();
hashMap.put("col1", drawableIds[i]);
hashMap.put("col2", this.getResources().getString(nameIds[i]));
hashMap.put("col3",this.getResources().getString(msgIds[i]) );
list.add(hashMap);
}
return list;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView=(GridView)findViewById(R.id.gridView);
SimpleAdapter simpleAdapter=new SimpleAdapter(this, generateDataList(), R.layout.grid_row, new String[]{"col1","col2","col3"}, new int[] {R.id.imageView,R.id.textView02,R.id.textView03});
gridView.setAdapter(simpleAdapter);
gridView.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
TextView textView=(TextView)findViewById(R.id.textView);
LinearLayout linearLayout=(LinearLayout)arg1;
TextView textView2=(TextView)linearLayout.getChildAt(1);
TextView textView3=(TextView)linearLayout.getChildAt(2);
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(textView2.getText());
stringBuilder.append(" ");
stringBuilder.append(textView3.getText());
textView.setText(stringBuilder.toString());

}


@Override
public void onNothingSelected(AdapterView<?> arg0) {

}

});
gridView.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView textView=(TextView)findViewById(R.id.textView);
LinearLayout linearLayout=(LinearLayout)arg1;
TextView textView2=(TextView)linearLayout.getChildAt(1);
TextView textView3=(TextView)linearLayout.getChildAt(2);
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(textView2.getText());
stringBuilder.append(" ");
stringBuilder.append(textView3.getText());
textView.setText(stringBuilder.toString());

}

});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

main.xml的代码如下:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.gridview.MainActivity" >


    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/hello_world"
        android:textColor="@color/white"
        android:textSize="24dip"
        />
<GridView 
   android:id="@+id/gridView"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:verticalSpacing="5dip"
   android:stretchMode="columnWidth"
   />
</RelativeLayout>

grid_row.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView 
        android:id="@+id/imageView"
        android:scaleType="fitXY"
        android:layout_width="100dip" 
        android:layout_height="98dip"
        
        />


    <TextView
        android:id="@+id/textView02"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="24dip"
        android:paddingLeft="5dip"
       />
    <TextView
        android:id="@+id/textView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="24dip"
        android:paddingLeft="5dip"
       />
</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值