现在的位置: 首页 > 综合 > 正文

android listview实现表格样式

2013年08月17日 ⁄ 综合 ⁄ 共 4529字 ⁄ 字号 评论关闭

      初学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,希望各位提出宝贵意见。

抱歉!评论已关闭.