android listview实现表格样式
初学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,希望各位提出宝贵意见。
更多相关文章
- 解决Android编辑框在全屏模式下无法检测布局变化的问题
- 四.Android六种布局详细讲解
- Android ConstraintLayout 约束布局
- android表格数据移动功能
- Android Layout Tricks #3: Optimize by merging(Android 布局技
- android一种较为复杂的布局参考(xml文件)
- android使用同一个RecyclerView实现两种不同Item布局
- Android Studio [相对布局RelativeLayout]
- [中英文对照]android Designing for TV(一) ------ Optimizing L