gridview--基本的gridview
2015-12-20 21:37
225 查看
GridView 元素距离设定
因为该设定比较简单 防止以后忘记 所以贴 供自己查阅
1. 布局:main.xml
Java代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:horizontalSpacing="50dp"
android:verticalSpacing="50dp"
/>
</RelativeLayout>
2. *.jave: GriUsage.java
Java代码
public class GridUsage extends Activity {
GridView grid;
ImageAdapter iAdapter;
String[] text = {
"one","two","three","four","five","six","seven","eight","nine","ten"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
grid = (GridView)findViewById(R.id.grid);
iAdapter = new ImageAdapter(this);
grid.setAdapter(iAdapter);
grid.setNumColumns(3);
}
public class ImageAdapter extends BaseAdapter {
Activity activity;
public ImageAdapter(Activity a){
activity = a;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return text.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv;
if(convertView == null){
tv = new TextView(activity);
}
else {
tv = (TextView)convertView;
}
tv.setSingleLine(true);
tv.setBackgroundResource(R.drawable.back);
tv.setGravity(Gravity.CENTER);
tv.setText(text[position]);
return tv;
}
}
}
3. emulator 运行截图:
that's all. any idea or other are welcome!!!!
因为该设定比较简单 防止以后忘记 所以贴 供自己查阅
1. 布局:main.xml
Java代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:horizontalSpacing="50dp"
android:verticalSpacing="50dp"
/>
</RelativeLayout>
2. *.jave: GriUsage.java
Java代码
public class GridUsage extends Activity {
GridView grid;
ImageAdapter iAdapter;
String[] text = {
"one","two","three","four","five","six","seven","eight","nine","ten"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
grid = (GridView)findViewById(R.id.grid);
iAdapter = new ImageAdapter(this);
grid.setAdapter(iAdapter);
grid.setNumColumns(3);
}
public class ImageAdapter extends BaseAdapter {
Activity activity;
public ImageAdapter(Activity a){
activity = a;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return text.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv;
if(convertView == null){
tv = new TextView(activity);
}
else {
tv = (TextView)convertView;
}
tv.setSingleLine(true);
tv.setBackgroundResource(R.drawable.back);
tv.setGravity(Gravity.CENTER);
tv.setText(text[position]);
return tv;
}
}
}
3. emulator 运行截图:
that's all. any idea or other are welcome!!!!
相关文章推荐
- boa安装
- 源码安装lua
- 详解WebApp与Native App的区别(转)
- 五、在myeclipse中查询与添加索引实例
- DZNEmptyDataSet
- Android中Parcelable接口用法
- Android 怎么录制视频
- 软件工程总结
- html知识小结
- Android中Activity启动模式详解(转)
- JavaScript 浮点数及运算精度调整总结,浮点数计算不准确怎么办
- 狼道文化
- online_judge_1119
- windows下sublime编译调试c++的配置代码
- DIY_hlstudio_WIN7PE【69M】网络版【89M】
- web测试方法总结(转载)
- Android ListView性能提升技巧(转)
- SqlHelper
- 《挑战程序设计竞赛》2.6 数学问题-快速幂运算 POJ1995
- 利用Fiddler抓取websocket包