android 动态加载View

本文介绍了一种在Android应用中动态加载布局的方法。通过在主布局文件中预留位置,并使用Java代码根据需要动态添加子布局视图,实现了灵活的UI展示。文章提供了具体的XML布局文件示例及对应的Java代码片段。

   在布局中动态加载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);
            
        }

 

转载于:https://www.cnblogs.com/lgy0069/p/5396358.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值