首先介绍功能,我要实现动态加载布局的效果,之前是采用的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();
}
}