初学android,试着写了个用listview实现的表格式样,先看下表格:

首先看两个布局文件,mylistview.xml中的内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<!--实现滚动-->

<HorizontalScrollView
android:layoutDirection="ltr"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView
android:id="@+id/mylist"
android:layout_width="fill_parent"
android:dividerHeight="1px"
android:divider="#FF909090"//实现水平分隔线
android:layout_height="fill_parent"
></ListView>
</HorizontalScrollView>"
</RelativeLayout>

列表项中的布局:

<?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" >
<TableLayout
android:id="@+id/mylistItem"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<!--每列后的竖线-->
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View
android:layout_width="0.5dp"
android:background="#FF909090"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View
android:layout_width="0.5dp"
android:background="#FF909090"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View
android:layout_width="0.5dp"
android:background="#FF909090"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/school"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View
android:layout_width="0.5dp"
android:background="#FF909090"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/high"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View
android:layout_width="0.5dp"
android:background="#FF909090"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>

</LinearLayout>

后台的实现是使用了adapter的getview。在activity文件中初始化适配器和列表数据,现在贴出主要的getview方法:

@Override
public View getView(int position,View contentView,ViewGroup parent){

if (null == contentView) {
contentView = mInflater.inflate(R.layout.list_item,null);
}
if (views[position] != null) {//对象池,防止getView方法重复调用
return views[position];
}

Display display = context.getWindowManager().getDefaultDisplay();
this.width = display.getWidth();


String[] data = table.get(position);
TextView title = (TextView) contentView.findViewById(R.id.name);
TextView age = (TextView) contentView.findViewById(R.id.age);
TextView sex = (TextView) contentView.findViewById(R.id.sex);
TextView school = (TextView) contentView.findViewById(R.id.school);
TextView high = (TextView) contentView.findViewById(R.id.high);
System.out.println("position============"+position);
if (position == 0) {//标题行居中
contentView.setBackgroundColor(contentView.getResources().getColor(R.color.deep_gray));
title.setGravity(Gravity.CENTER);
age.setGravity(Gravity.CENTER);
sex.setGravity(Gravity.CENTER);
school.setGravity(Gravity.CENTER);
high.setGravity(Gravity.CENTER);
} else if (position % 2 == 0) {//奇偶行背景色
contentView.setBackgroundColor(contentView.getResources().getColor(R.color.palegreen));
}else {
contentView.setBackgroundColor(contentView.getResources().getColor(R.color.lightgreen));
}
title.setWidth((int)(width*0.2));//width是屏幕宽度,在此实现宽度的百分比
age.setWidth((int)(width*0.2));
sex.setWidth((int)(width*0.2));
school.setWidth((int)(width*0.2));
high.setWidth((int)(width*0.2));
title.setText(data[0]);
age.setText(data[1]);
sex.setText(data[2]);
school.setText(data[3]);
high.setText(data[4]);
views[position] = contentView;

//当然在这还可以对行加上点击事件
return views[position];

}

以上就是实现的主要的代码,刚学习android,希望各位提出宝贵意见。

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. python list.sort()根据多个关键字排序的方法实现
  3. Android(安卓)AppWidget系统框架
  4. 【Android】如何用MediaPlayer实现一个简单的音视频播放器
  5. Android(安卓)PinyinIME 源码笔记 -- 0. 简介
  6. android中textView周围显示图片
  7. 粘贴复制Android复制和粘贴的实现
  8. Android(安卓)商显会议平板聚光灯效果实现
  9. Android实现圆角弹框功能

随机推荐

  1. 使用Javascript判断浏览器终端设备(PC、I
  2. 【读书笔记】【Android 开发艺术探索】第
  3. android SDK系统图片资源的路径。
  4. android 相对定位布局方向
  5. Android启动画面实现
  6. android 删除的警告对话框
  7. android usb host 读写USB设备
  8. Android下监听Home键
  9. 修改Android EditText光标颜色
  10. android九宫格