表格是很常用的控件,Android本身提供了TableLayout供布局实现。但本文介绍另外一种思路,用动态布局的方式实现,这种方式更灵活,内容、样式能高度扩展,熟练的人可随意运用到任何视图复用的场景。使用滚动条避免显示不完全问题。

效果图:

%title插图%num

核心代码如下:

public class DriveRecordAcivity extends Fragment{
private View view = null;
private LinearLayout wr_areas;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view=inflater.inflate(R.layout.driverecord_activity, null);
wr_areas=(LinearLayout) view.findViewById(R.id.wr_areas);
if(ApplicationUtils.getCheckedFcRecord(new DBAdapter(view.getContext()))!=null){
List<CheckedFcRecord> titleData=ApplicationUtils.getCheckedFcRecord(new DBAdapter(view.getContext()));
showData(titleData);
}
return view;
}

/**
* 发车数据动态添加状态
*/
private void showData(List<CheckedFcRecord> titleData) {
for (int i = 0; i < titleData.size(); i++) {
final CheckedFcRecord pojo = titleData.get(i);
LinearLayout llWashingRoomItem = new LinearLayout(view.getContext());
llWashingRoomItem.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
llWashingRoomItem = (LinearLayout) getActivity().getLayoutInflater().inflate(R.layout.checkedfcrecord_template, null);
TextView time = (TextView) llWashingRoomItem.findViewById(R.id.time);
TextView vhclNo = (TextView) llWashingRoomItem.findViewById(R.id.vhclNo);
TextView jpy = (TextView) llWashingRoomItem.findViewById(R.id.jpy);
TextView ticket = (TextView) llWashingRoomItem.findViewById(R.id.ticket);
time.setText(DateTools.getStringFromDate(pojo.getFcTime(),null));
vhclNo.setText(pojo.getVhcl_no());
jpy.setText(pojo.getJsy_name());
//Integer类型需要转换用.toString()不然报错
ticket.setText(pojo.getJps().toString());
//动态设置layout_weight权重设置表格宽度
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.6f);
time.setLayoutParams(lp);
lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
vhclNo.setLayoutParams(lp);
lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 0.8f);
jpy.setLayoutParams(lp);
lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 0.8f);
ticket.setLayoutParams(lp);
wr_areas.addView(llWashingRoomItem);
}
}
}
上面是运用到项目中,动态获取数据。测试源码中的是手动添加的数据。

xml布局如下:

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”@color/main_bg”
android:orientation=”vertical”
tools:context=”.WashingRoomMonitor” >
<RelativeLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:gravity=”center” >
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:padding=”10dp”
android:text=”测试记录”
android:textColor=”@color/white”
android:textSize=”@dimen/text_size_20″ />
</RelativeLayout>

<TextView
android:layout_width=”match_parent”
android:layout_height=”1dp”
android:background=”@color/split_line2″ />

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal” >

<TextView
android:text=”时区”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1.5″
android:gravity=”center”
android:padding=”5dp”
android:textColor=”@color/white”/>

<TextView
android:layout_width=”1dp”
android:layout_height=”match_parent”
android:background=”@color/split_line2″ />

<TextView
android:text=”序号”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:gravity=”center”
android:padding=”5dp”
android:textColor=”@color/white” />

<TextView
android:layout_width=”1dp”
android:layout_height=”match_parent”
android:background=”@color/split_line2″ />

<TextView
android:text=”人员”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:gravity=”center”
android:padding=”5dp”
android:textColor=”@color/white” />

<TextView
android:layout_width=”1dp”
android:layout_height=”match_parent”
android:background=”@color/split_line2″ />

<TextView
android:text=”数量”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:gravity=”center”
android:padding=”5dp”
android:textColor=”@color/white” />
</LinearLayout>

<TextView
android:layout_width=”match_parent”
android:layout_height=”1dp”
android:background=”@color/split_line2″ />

<ScrollView
android:layout_width=”match_parent”
android:layout_height=”wrap_content” >

<LinearLayout
android:id=”@+id/wr_areas”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical” >
</LinearLayout>
</ScrollView>

</LinearLayout>

%title插图%num
定义可重用的视图部分,xml文件如下:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”
tools:context=”.MainActivity” >

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal” >

<TextView
android:id=”@+id/tv_wr_areaname”
android:text=”6″
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1.5″
android:gravity=”center”
android:padding=”5dp”
android:textColor=”@color/white” />

<TextView
android:layout_width=”1dp”
android:layout_height=”match_parent”
android:background=”@color/split_line2″ />

<TextView
android:id=”@+id/tv_wr_mt_man”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:gravity=”center”
android:padding=”5dp”
android:text=”6″
android:textColor=”@color/white” />

<TextView
android:layout_width=”1dp”
android:layout_height=”match_parent”
android:background=”@color/split_line2″ />

<TextView
android:id=”@+id/tv_wr_dc_man”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:gravity=”center”
android:padding=”5dp”
android:text=”6″
android:textColor=”@color/white” />

<TextView
android:layout_width=”1dp”
android:layout_height=”match_parent”
android:background=”@color/split_line2″ />

<TextView
android:id=”@+id/tv_wr_xbc_man”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:gravity=”center”
android:padding=”5dp”
android:text=”6″
android:textColor=”@color/white” />

<TextView
android:layout_width=”1dp”
android:layout_height=”match_parent”
android:background=”@color/split_line2″ />

</LinearLayout>

<TextView
android:layout_width=”match_parent”
android:layout_height=”1dp”
android:background=”@color/split_line2″ />

</LinearLayout>
%title插图%num