Layout文件gridview_demo.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<GridView xmlns:android=""
-
android:id="@+id/gridViewDemo"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:numColumns="auto_fit"
-
android:gravity="center"
-
android:textAlignment="center"
-
android:columnWidth="50dp"
-
android:stretchMode="columnWidth" >
-
</GridView>
GridViewActivity类:
-
package com.example.twiter;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.AdapterView;
-
import android.widget.AdapterView.OnItemClickListener;
-
import android.widget.ArrayAdapter;
-
import android.widget.GridView;
-
import android.widget.TextView;
-
import android.widget.Toast;
-
-
public class GridViewActivity extends Activity {
-
-
GridView gridView;
-
-
static final String[] numbers = new String[] {
-
"A", "B", "C", "D", "E",
-
"F", "G", "H", "I", "J",
-
"K", "L", "M", "N", "O",
-
"P", "Q", "R", "S", "T",
-
"U", "V", "W", "X", "Y", "Z"};
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.gridview_demo);
-
-
gridView = (GridView) findViewById(R.id.gridViewDemo);
-
-
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
-
android.R.layout.simple_list_item_1, numbers);
-
-
gridView.setAdapter(adapter);
-
-
gridView.setOnItemClickListener(new OnItemClickListener() {
-
public void onItemClick(AdapterView<?> parent, View v,
-
int position, long id) {
-
Toast.makeText(getApplicationContext(),
-
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
-
}
-
});
-
}
-
-
}
AndroidManifest.xml文件中添加以下行:
-
<activity android:name="com.example.twiter.GridViewActivity">
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
效果图:
阅读(3393) | 评论(0) | 转发(1) |