android 动态加载自定义控件

        首先介绍功能,我要实现动态加载布局的效果,之前是采用的new组件的办法来实现,但是android内存有限,new的对象会达到500多个,为了减少new的对象,我决定使用xml布局代替new的对象。(后来经过对比测试,发现二者的差别不大,消耗的内存差不多,不明白其中的原理)

       自定义控件的布局:

      

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/textView1"
        android:text="Button" />

</RelativeLayout>

        自定义控件 java类:

public class ViewMY extends LinearLayout{
	public ViewMY(Context context) {
		super(context);
		LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = mInflater.inflate(R.layout.activity_main, null);
		this.addView(v);
	}

	public ViewMY(Context context, AttributeSet attrs) {
		super(context, attrs);
		 
	}
	@Override
	public void setLayoutParams(android.view.ViewGroup.LayoutParams params) {
		//参数设置不合理,显示效果很差
		params.width=300;
		params.height=100;
		super.setLayoutParams(params);
	}
}

Activity布局:

<?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:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/scrollView1_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

</LinearLayout>
MainActivity:

开启一条线程定时发送消息,来模拟加载组件的效果,自定义组件使用ScrollView展示,注意,这里ScrollView内部必须要有 一个子布局。把自定义控件new出来添加到子布局中。就可以实现每隔5秒中产生一个控件并设置相对应的参数。

public class MainActivity extends Activity {

	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			if(msg.what==1){
				ViewMY v = new ViewMY(MainActivity.this);
				TextView tv = (TextView) v.findViewById(R.id.textView1);
				tv.setText("abcdb   ="+count);
				v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,600));
				ll.addView(v);
			}
		};
	};
	private ScrollView sv;
	private LinearLayout ll;
	int count=0;
	boolean isRun;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		sv = (ScrollView) findViewById(R.id.scrollView1);
		ll = (LinearLayout) findViewById(R.id.scrollView1_layout);
		isRun=true;
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(isRun){
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					handler.sendEmptyMessage(1);
					count++;
					if(count==10){
						isRun=false;
					}
				}
			}
		}).start();
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值