标签:
在布局中动态加载View
1.主布局的funddatail.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:scrollbars="vertical" > <LinearLayout android:id="@+id/fund_manger_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> </LinearLayout>
2.动态加载的Item fund_manger_detail.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="@color/photo_day_bg" android:padding="5dp" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical" android:text="姓名" android:textColor="@color/widget_summary" android:textSize="@dimen/comment_text_size" /> <TextView android:id="@+id/fund_basedata_manger_tv01" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:gravity="left" android:textColor="@color/black" android:textSize="@dimen/comment_text_size" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="horizontal" android:padding="5dp" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical" android:text="学历" android:textColor="@color/widget_summary" android:textSize="@dimen/comment_text_size" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:orientation="horizontal" > <TextView android:id="@+id/fund_basedata_manger_tv02" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:textColor="@color/black" android:textSize="@dimen/comment_text_size" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:text="任职日期" android:textColor="@color/widget_summary" android:textSize="@dimen/comment_text_size" /> <TextView android:id="@+id/fund_basedata_manger_tv03" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical" android:textColor="@color/black" android:textSize="@dimen/comment_text_size" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/photo_day_bg" android:orientation="horizontal" android:padding="5dp" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical" android:text="简介" android:textColor="@color/widget_summary" android:textSize="@dimen/comment_text_size" /> <TextView android:id="@+id/fund_basedata_manger_tv04" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:gravity="left" android:textColor="@color/black" android:textSize="@dimen/comment_text_size" /> </LinearLayout> </LinearLayout>
3.在类中动态的加载:
ViewGroup.LayoutParams prarms = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); fund_manger_layout.removeAllViews(); if(baseinfos.size()<0){ manger_layout_name.setVisibility(View.GONE); } for(int i=0;i<baseinfos.size();i++){ View mangerlayout=View.inflate(activity, R.layout.fund_manger_detail, null); fund_basedata_manger_tv01=(TextView) mangerlayout.findViewById(R.id.fund_basedata_manger_tv01); fund_basedata_manger_tv02=(TextView) mangerlayout.findViewById(R.id.fund_basedata_manger_tv02); fund_basedata_manger_tv03=(TextView) mangerlayout.findViewById(R.id.fund_basedata_manger_tv03); fund_basedata_manger_tv04=(TextView) mangerlayout.findViewById(R.id.fund_basedata_manger_tv04); fund_basedata_manger_tv01.setText(baseinfos.get(i).getManger_name()); fund_basedata_manger_tv02.setText(baseinfos.get(i).getEducation()); fund_basedata_manger_tv03.setText(baseinfos.get(i).getTake_date()); fund_basedata_manger_tv04.setText(baseinfos.get(i).getDetail()); fund_manger_layout.addView(mangerlayout, prarms); }
标签:
原文地址:http://www.cnblogs.com/lgy0069/p/5396358.html