android 动态加载自定义控件
2013-12-29 16:46
330 查看
首先介绍功能,我要实现动态加载布局的效果,之前是采用的new组件的办法来实现,但是android内存有限,new的对象会达到500多个,为了减少new的对象,我决定使用xml布局代替new的对象。(后来经过对比测试,发现二者的差别不大,消耗的内存差不多,不明白其中的原理)
自定义控件的布局:
自定义控件 java类:
Activity布局:
开启一条线程定时发送消息,来模拟加载组件的效果,自定义组件使用ScrollView展示,注意,这里ScrollView内部必须要有 一个子布局。把自定义控件new出来添加到子布局中。就可以实现每隔5秒中产生一个控件并设置相对应的参数。
自定义控件的布局:
<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(); } }
相关文章推荐
- Android安全讲座第八层[二] 替换已经安装后的应用的dex文件
- 不重造车轮,Android开发必备库
- Android 布局中长度单位的深入研究
- 最新Android SDK更新hosts 2013年12月更新
- Android笔记:SurfaceView与SurfaceHolder对象
- 更改android AVD模拟器创建路径位置
- Android 线程池对象-ThreadPoolExecutor浅析
- Android异步加载图像(含线程池,缓存方法)
- Android笔记之调用
- Android对话框风格Activity的设置
- android中xml文件的生成与读取(Pull)
- Android 几种屏幕间跳转的跳转Intent Bundle
- Android第一个程序及问题解决
- 直接修改Android软件数据库来改变软件设置实例一则
- 【Android学习】四种布局方式
- 浅析Android Camera开发中的三个尺寸和三种变形 (贡献一个自适配Picturesize和Previewsize的工具类)
- android raw文件夹导入数据库
- android学习总结与杂想,第一周
- android ViewFlipper
- Android添加垂直滚动ScrollView