Android(安卓)利用addView 让Activity 动态加入多个View 物件
16lz
2021-01-25
在Android 中,
你可以利用排版View的 addView 函数,
将动态产生的View 物件加入到排版View 中,
范例如下 :
main.xml 部分内容
Java 程式码
public class helloWorld extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView( R.layout.main ); // 取得LinearLayout 物件 LinearLayout ll = (LinearLayout)findViewById(R.id.viewObj); // 将TextView 加入到LinearLayout 中 TextView tv = new TextView(this); tv.setText("Hello World"); ll. addView ( tv ); // 将Button 1 加入到LinearLayout 中 Button b1 = new Button(this); b1.setText("取消"); ll. addView ( b1 ); // 将Button 2 加入到LinearLayout 中 Button b2 = new Button(this); b2.setText("确定"); ll. addView ( b2 ); // 从LinearLayout 中移除Button 1 ll. removeView ( b1 ); }}
范例结果 :
原文链接:http://tomkuo139.blogspot.com/2010/01/android-addview-activity-view.html
更多相关文章
- 【Android(安卓)开发教程】ListView的总结范例
- 【Android(安卓)开发教程】添加ActionBar物件
- Android中对TextView中的部分内容的字体样式的设置方法
- Google Android(安卓)SDK开发范例大全
- 《阿里巴巴Android开发手册》v1.0.1更新,优化部分内容和示例代码
- 为什么Android的Adapter中,bindview被调用了多次
- 《解析 Google Android(安卓)DSK-智能手机开发范例手册》.(薛显
- Android(安卓)基础:SharePreferences知识总结和在“登录”界面的
- Google Android(安卓)SDK开发范例------------20141119