Android表格控件动态生成表格

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

效果图:

核心代码如下:

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)
  • 4
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值