其实该例子可以用listview做更好。
先看效果图:
红色框框部分就是tablelayout。
实现过程:
tableLayout = (TableLayout) findViewById(R.id.table_risk_profile);
tableLayout.setStretchAllColumns(true);//设置所有的item都可伸缩扩展
tableLayout.setDividerDrawable(getResources().getDrawable(R.drawable.bonus_list_item_divider));//这个就是中间的虚线
tableLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);//设置分割线为中间显示
构建过程就很简单了:
public void buildTable(){
int length = riskProfile.dataList.size();//根据数据,判断行数
for (int i = 0; i < length;i++){
RiskItem item = riskProfile.dataList.get(i);//获取单行数据
View layout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.risk_profile_table_item,null);//布局打气筒获取单行对象
TextView name = (TextView) layout.findViewById(R.id.name);
TextView value = (TextView) layout.findViewById(R.id.value);
TextView statue = (TextView) layout.findViewById(R.id.statue);
name.setText(item.name);
value.setText(item.show_value);
/*根据状态字段,判断显示内容与颜色*/
if ("偏高".equals(item.status_value) || "异常".equals(item.status_value)){
statue.setTextColor(getResources().getColor(R.color.light_red));
}else if("正常".equals(item.status_value)){
statue.setTextColor(getResources().getColor(R.color.light_green));
}else{
statue.setTextColor(getResources().getColor(R.color.dark_yellow));
}
statue.setText(item.status_value);
tableLayout.addView(layout);//将这一行加入表格中
}
}
布局打气筒中的xml布局,就是三个textview,水平等比排放,weight = 1,这个很简单就不贴代码了。
tablelayout设置也没什么特殊:
<TableLayout
android:id="@+id/table_risk_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/white"/>
以上。