技术点总结:首先在layout的界面xml文件中,设置LinearLayout容器内容显示格式,然后自定义类(ReserveOrderDetail)将获取到的数据,添加到序列化列表List<ReserveOrderDetail> orderDetails列表中,然后将orderDetails数据给到类(ReserveOrderDetail)对象orderDetail1,再通过orderDetail1即可变量遍历出来,再将数据设置到指定的layout中,然后逐一设置到动态生成容器当中即可完成动态生成控件显示出来。
主要实现代码如下:
//================== 动态生成控件==========================
totalMoney = BigDecimal.valueOf(0);//清空总金额
llOrderDetail.removeAllViews();//清空布局容器
for (ReserveOrderDetail orderDetail1:orderDetails){//遍历orderDetail1 中的数据
//计算总金额
totalMoney=totalMoney.add(orderDetail1.getDetailMoney());
//动态添加控件
//===1、添加外部LinerLayout
LinearLayout layout=new LinearLayout(mActivityContext);
//==1.1、设置LinearLayout的布局
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(//布局参数设置
ViewGroup.LayoutParams.MATCH_PARENT,//宽度内容占满整行
ViewGroup.LayoutParams.WRAP_CONTENT//高度内容自适应
);
layout.setLayoutParams(layoutParams);//给layout设置布局参数
//===1.2、设置为横向布局
layout.setOrientation(LinearLayout.HORIZONTAL);
//====2、TextView 显示明显描述
TextView tvDescribe=new TextView(mActivityContext);//绑定当前窗口
LinearLayout.LayoutParams tvLayoutParams=new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
tvDescribe.setLayoutParams(tvLayoutParams);
tvDescribe.setTextSize(16);//设置字体大小
tvDescribe.setText(orderDetail1.getDescribe());
layout.addView(tvDescribe);
//=====3、TextView 中间的虚线
TextView tvDashLine = new TextView(mActivityContext);
LinearLayout.LayoutParams tvLayoutParamsDashLine = new LinearLayout.LayoutParams(
0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);//宽 高 权重
tvDashLine.setLayoutParams(tvLayoutParamsDashLine);
tvDashLine.setBackgroundResource(R.drawable.shape_line_dash);
layout.addView(tvDashLine);
//======4、TextView价格信息
TextView tvMoney = new TextView(mActivityContext);
tvMoney.setLayoutParams(tvLayoutParams);
tvMoney.setTextSize(16);//设置字体大小
tvMoney.setText(String.format(Locale.getDefault(), "¥%d", orderDetail1.getDetailMoney().intValue()));
layout.addView(tvMoney);
//==1.3、将容器添加到外部容器llOrderDetail
llOrderDetail.addView(layout);
}
效果: