后台动态添加布局文件、控件与动态设置属性
http://blog.csdn.net/aojiancc2/article/details/40510547
原布局文件
- <ScrollView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:scrollbars="vertical"
- android:overScrollMode="never"
- android:fadingEdge="none"
- >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <LinearLayout
- android:layout_marginTop="9dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingLeft="6dp"
- android:paddingBottom="9dp"
- android:background="#ff00ff"
- >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="4dp"
- android:src="@drawable/shipindex"/>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="8dp"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="8dp"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#7d8b9b"
- android:text="乔泰27"
- android:textSize="17dp"
- />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="12dp"
- android:layout_marginTop="4dp"
- android:src="@drawable/position"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#b6b6b6"
- android:layout_marginLeft="1dp"
- android:text="广州"
- android:textSize="12dp"
- />
- </LinearLayout>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#cdcdcd"
- android:text="在线时间"
- android:textSize="11dp"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#cdcdcd"
- android:layout_marginLeft="8dp"
- android:text="19285小时28分"
- android:textSize="11dp"
- />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- <LinearLayout
- android:layout_marginTop="5dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <TextView
- android:layout_width="35dp"
- android:layout_height="wrap_content"
- android:textColor="#B7BEC1"
- android:textSize="13dp"
- android:layout_marginTop="1dp"
- android:layout_gravity="bottom"
- android:text="油仓" />
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:gravity="center_vertical"
- android:orientation="horizontal">
- <RelativeLayout
- android:layout_width="156dp"
- android:layout_height="13dp"
- android:background="#E5E4E3"
- >
- <LinearLayout
- android:layout_width="100dp"
- android:layout_height="13dp"
- android:background="#FF9B58"
- android:layout_alignParentLeft="true"
- android:gravity="center_horizontal"
- >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center_vertical"
- android:orientation="horizontal"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:textColor="#ffffff"
- android:textSize="9dp"
- android:text="80%"
- />
- </LinearLayout>
- </LinearLayout>
- </RelativeLayout>
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- </ScrollView>
后台生成代码
- public class IndexFillData {
- /*做这个需要后台生成的布局文件,写点前台就写点后台,
- * 不然太多了一起写嵌套太多太麻烦了*/
- FragmentActivity fagb;
- public IndexFillData(FragmentActivity _fa,IndexDataModel _datamodel)
- {
- fagb = _fa;
- exefillDatahandler(_datamodel,_fa);
- }
- //http://jingyan.baidu.com/article/63acb44adbb74561fcc17e98.html
- private void fillHeadData(IndexDataModel _datamodel)
- {
- TextView tv = (TextView)fagb.findViewById(R.id.index_head_comapny);
- tv.setText(_datamodel.getCompanyName());
- TextView tv_shipcount = (TextView)fagb.findViewById(R.id.index_head_shipcount);
- tv_shipcount.setText(_datamodel.getShipCount()+"");
- TextView tv_totalMileage = (TextView)fagb.findViewById(R.id.index_head_totalMileage);
- tv_totalMileage.setText(_datamodel.getTotalMileage()+"");
- }
- private void fillOvilData(IndexDataModel _datamodel)
- {
- CreateOvilRoot(_datamodel);
- /*List<shipInfos> silist =_datamodel.getShipInfos();
- for(int i=0;i<silist.size();i++)
- {
- shipInfos si = silist.get(i);
- }*/
- }
- private void CreateOvilRoot(IndexDataModel _datamodel)
- {
- System.out.println("CreateOvilRoot1111:");
- LinearLayout ll_root = new LinearLayout(fagb.getApplicationContext());
- LinearLayout.LayoutParams ll_root_param = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
- ll_root.setLayoutParams(ll_root_param);
- ll_root.setOrientation(LinearLayout.VERTICAL);
- LinearLayout ll_f = new LinearLayout(fagb.getApplicationContext());
- LinearLayout.LayoutParams lp_param = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
- lp_param.setMargins(0,Dp2Px(9),0,0);
- ll_f.setPadding(6, 0, 0, 9);
- ll_f.setLayoutParams(lp_param);
- ll_f.setBackgroundColor(Color.parseColor("#ffffff"));
- LinearLayout ll_f_c = new LinearLayout(fagb.getApplicationContext());
- LinearLayout.LayoutParams ll_f_c_param = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
- ll_f_c.setLayoutParams(ll_f_c_param);
- ll_f_c.setOrientation(LinearLayout.VERTICAL);
- ll_f.addView(ll_f_c);
- ll_root.addView(ll_f);
- CreateChilOne(ll_f_c);
- new IndexFillDataTwo(fagb,_datamodel).CreateChilOne_2(ll_f_c);
- System.out.println("CreateOvilRoot99999:");
- ScrollView sv = (ScrollView)fagb.findViewById(R.id.index_sv);
- sv.addView(ll_root);
- }
- private void CreateChilOne(LinearLayout _parent)
- {
- LinearLayout ll_f = new LinearLayout(fagb.getApplicationContext());
- LinearLayout.LayoutParams ll_f_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- ll_f.setLayoutParams(ll_f_param);
- ll_f.setOrientation(LinearLayout.HORIZONTAL);
- ImageView shiiw = new ImageView(fagb.getApplicationContext());
- LinearLayout.LayoutParams llfcc_cimg_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- llfcc_cimg_param.setMargins(0, Dp2Px(4), 0, 0);
- shiiw.setLayoutParams(llfcc_cimg_param);
- shiiw.setImageDrawable(fagb.getResources().getDrawable(R.drawable.shipindex));
- ll_f.addView(shiiw);
- LinearLayout ll_c_f = new LinearLayout(fagb.getApplicationContext());
- LinearLayout.LayoutParams ll_f_c_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- ll_c_f.setLayoutParams(ll_f_c_param);
- ll_c_f.setPadding(Dp2Px(8), 0, 0, 0);
- ll_c_f.setOrientation(LinearLayout.VERTICAL);
- ll_f.addView(ll_c_f);
- _parent.addView(ll_f);
- CreateChilTwo(ll_c_f);
- }
- private void CreateChilTwo(LinearLayout _parent)
- {
- LinearLayout ll_c_f = new LinearLayout(fagb.getApplicationContext());
- LinearLayout.LayoutParams ll_f_c_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- ll_c_f.setLayoutParams(ll_f_c_param);
- ll_c_f.setPadding(Dp2Px(8), 0, 0, 0);
- ll_c_f.setOrientation(LinearLayout.VERTICAL);
- _parent.addView(ll_c_f);
- CreateChilThree(ll_c_f);
- }
- private void CreateChilThree(LinearLayout _parent)
- {
- LinearLayout ll_c_f = new LinearLayout(fagb.getApplicationContext());
- LinearLayout.LayoutParams ll_f_c_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- ll_c_f.setLayoutParams(ll_f_c_param);
- ll_c_f.setOrientation(LinearLayout.HORIZONTAL);
- LinearLayout ll_c_f2 = new LinearLayout(fagb.getApplicationContext());
- ll_c_f2.setLayoutParams(ll_f_c_param);
- ll_c_f2.setOrientation(LinearLayout.HORIZONTAL);
- _parent.addView(ll_c_f);
- _parent.addView(ll_c_f2);
- CreateChilFour_1(ll_c_f);
- CreateChilFour_2(ll_c_f2);
- }
- private void CreateChilFour_1(LinearLayout _parent)
- {
- LinearLayout.LayoutParams tv_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- TextView tv = new TextView(fagb);
- tv.setTextColor(Color.parseColor("#7d8b9b"));
- tv.setLayoutParams(tv_param);
- tv.setTextSize(17);
- tv.setText("乔泰27");
- ImageView ivarrow = new ImageView(fagb.getApplicationContext());
- LinearLayout.LayoutParams cimg_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- cimg_param.setMargins(Dp2Px(12), Dp2Px(4), 0, 0);
- ivarrow.setLayoutParams(cimg_param);
- ivarrow.setImageDrawable(fagb.getResources().getDrawable(R.drawable.position));
- TextView tv_position = new TextView(fagb);
- tv_position.setTextColor(Color.parseColor("#b6b6b6"));
- LinearLayout.LayoutParams tv_position_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- tv_position_param.setMargins(Dp2Px(1), 0, 0, 0);
- tv_position.setLayoutParams(tv_position_param);
- tv_position.setTextSize(12);
- tv_position.setText("广州");
- _parent.addView(tv);
- _parent.addView(ivarrow);
- _parent.addView(tv_position);
- }
- private void CreateChilFour_2(LinearLayout _parent)
- {
- LinearLayout.LayoutParams tv_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- TextView tv = new TextView(fagb);
- tv.setTextColor(Color.parseColor("#cdcdcd"));
- tv.setLayoutParams(tv_param);
- tv.setTextSize(11);
- tv.setText("在线时间");
- TextView tv_time = new TextView(fagb);
- tv_time.setTextColor(Color.parseColor("#cdcdcd"));
- LinearLayout.LayoutParams tv_time_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- tv_time_param.setMargins(Dp2Px(8), 0, 0, 0);
- tv_time.setLayoutParams(tv_time_param);
- tv_time.setTextSize(11);
- tv_time.setText("19285小时28分");
- _parent.addView(tv);
- _parent.addView(tv_time);
- }
- public int Dp2Px(float dp) {
- final float scale = fagb.getResources().getDisplayMetrics().density;
- return (int) (dp * scale + 0.5f);
- }
- private void fillData(IndexDataModel _datamodel)
- {
- fillHeadData(_datamodel);
- fillOvilData(_datamodel);
- }
- private void exefillDatahandler(IndexDataModel _datamodel,FragmentActivity _fa)
- {
- Message m = new Message();m.obj=_datamodel;
- Handler crefillDatahandler = crefillDatahandler(_fa);
- crefillDatahandler.sendMessage(m);
- }
- private Handler crefillDatahandler(FragmentActivity _fa){
- Handler fillDatahandler = new Handler(_fa.getMainLooper()){
- @Override
- public void handleMessage(Message msg) {
- try{
- fillData((IndexDataModel)msg.obj);
- }
- catch (Exception e) {
- }
- }
- };
- return fillDatahandler;
- }
- }
- public class IndexFillDataTwo {
- FragmentActivity fagb;
- public IndexFillDataTwo(FragmentActivity _fa,IndexDataModel _datamodel)
- {
- fagb = _fa;
- }
- public void CreateChilOne_2(LinearLayout _parent)
- {
- //油仓
- LinearLayout ll_f = new LinearLayout(fagb.getApplicationContext());
- LinearLayout.LayoutParams ll_f_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- ll_f_param.setMargins(0, Dp2Px(5), 0, 0);
- ll_f.setLayoutParams(ll_f_param);
- ll_f.setOrientation(LinearLayout.HORIZONTAL);
- _parent.addView(ll_f);
- CreateChilTwo(ll_f);
- }
- private void CreateChilTwo(LinearLayout _parent)
- {
- LinearLayout.LayoutParams tv_param = new LinearLayout.LayoutParams(
- Dp2Px(35),LinearLayout.LayoutParams.WRAP_CONTENT);
- tv_param.setMargins(0, Dp2Px(1), 0, 0);
- tv_param.gravity=Gravity.BOTTOM;
- TextView tv = new TextView(fagb);
- tv.setTextColor(Color.parseColor("#B7BEC1"));
- tv.setLayoutParams(tv_param);
- tv.setTextSize(13);
- tv.setText("油仓");
- LinearLayout ll_f = new LinearLayout(fagb.getApplicationContext());
- LinearLayout.LayoutParams ll_f_param = new LinearLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.MATCH_PARENT);
- ll_f.setLayoutParams(ll_f_param);
- ll_f.setOrientation(LinearLayout.HORIZONTAL);
- ll_f.setGravity(Gravity.CENTER_VERTICAL);
- _parent.addView(tv);
- _parent.addView(ll_f);
- CreateChilThree(ll_f);
- }
- private void CreateChilThree(LinearLayout _parent)
- {
- RelativeLayout ll_f = new RelativeLayout(fagb.getApplicationContext());
- LinearLayout.LayoutParams ll_f_param = new LinearLayout.LayoutParams(
- Dp2Px(156),Dp2Px(13));
- ll_f.setLayoutParams(ll_f_param);
- ll_f.setGravity(Gravity.CENTER_VERTICAL);
- ll_f.setBackgroundColor(Color.parseColor("#E5E4E3"));
- _parent.addView(ll_f);
- CreateChilFour(ll_f);
- }
- private void CreateChilFour(RelativeLayout _parent)
- {
- RelativeLayout.LayoutParams ll_f_param = new RelativeLayout.LayoutParams(
- Dp2Px(100),Dp2Px(13));
- ll_f_param.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
- LinearLayout ll_f = new LinearLayout(fagb.getApplicationContext());
- ll_f.setLayoutParams(ll_f_param);
- ll_f.setBackgroundColor(Color.parseColor("#FF9B58"));
- ll_f.setGravity(Gravity.CENTER_HORIZONTAL);
- _parent.addView(ll_f);
- CreateChilFive(ll_f);
- }
- private void CreateChilFive(LinearLayout _parent)
- {
- LinearLayout.LayoutParams ll_f_param = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
- LinearLayout ll_f = new LinearLayout(fagb.getApplicationContext());
- ll_f.setLayoutParams(ll_f_param);
- ll_f.setOrientation(LinearLayout.HORIZONTAL);
- ll_f.setBackgroundColor(Color.parseColor("#FF9B58"));
- ll_f.setGravity(Gravity.CENTER_VERTICAL);
- LinearLayout.LayoutParams tv_param = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
- tv_param.gravity=Gravity.CENTER_VERTICAL;
- TextView tv = new TextView(fagb);
- tv.setTextColor(Color.parseColor("#ffffff"));
- tv.setLayoutParams(tv_param);
- tv.setTextSize(9);
- tv.setText("80%");
- ll_f.addView(tv);
- _parent.addView(ll_f);
- }
- public int Dp2Px(float dp) {
- final float scale = fagb.getResources().getDisplayMetrics().density;
- return (int) (dp * scale + 0.5f);
- }
- }