Android中利用LinearLayout动态添加控件
来源:互联网 发布:adobe pdf mac 中文版 编辑:程序博客网 时间:2024/04/27 19:42
在androidUI布局中,一般都是利用xml来布局控件,这是比较方便和直观的,但是有时却需要动态生成,下面就举2个简单例子来说明怎么动态添加控件:
1.动态添加2个垂直排列的Button
final LinearLayout layout2=new LinearLayou(this);
定义一个LinearLayout ,参数为context在这儿即为this
layout2.setOrientation(LinearLayout.VERTICAL);
设置layout格式为vertical,竖直排列
2.在Button的click事件中动态添加一个button
与上一例子区别就是,在Button的OnClickListener中动态添加控件,需要注意的一点是 Button btn3=new Button(v.getContext);参数v.getContext为当前view的context,为什么例子1中用this作为context呢,呵呵,这是因为this即当前的activity,而activity又是context的子类,所以this就可以直接作为activity了。
查了下,context派生的类有:
java.lang.Object ↳android.content.Context Known Direct Subclasses ContextWrapper, MockContext
Known Indirect Subclasses
AbstractInputMethodService, Activity, ActivityGroup, AliasActivity, Application, ContextThemeWrapper, ExpandableListActivity, InputMethodService, IntentService, IsolatedContext, LauncherActivity, ListActivity, MockApplication, MutableContextWrapper, PreferenceActivity, RenamingDelegatingContext, Service, TabActivity
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加UI控件
- Android Linearlayout中动态添加ImageView
- Android动态设置布局,为LinearLayout动态添加子控件
- LinearLayout 动态添加TextView控件
- Android,在LinearLayout中动态添加TextView,几点疑问
- wpf中利用布局动态添加控件
- 安卓linearLayout动态添加控件
- android动态加载控件(LinearLayout)并获取动态添加的EditText里的值
- 动态中添加LinearLayout和RelativeLayout布局
- android 在布局中动态添加控件
- android代码中动态添加控件
- android中怎么动态添加TextView控件
- 利用反射动态添加控件
- 注册表操作
- 有点乱
- 顶点的纹理坐标
- [转]VC6自动提示功能失效的解决方法
- 技术博客开张
- Android中利用LinearLayout动态添加控件
- DShow中实现抓图的几种方法
- (转)一个Java程序员应该掌握的10项技能
- 基本原理
- C# 通过 HtmlDocument 操作HTML节点
- PROTEL 四层板设计
- 关于static指针的一个严重问题
- android rootfs制作
- 帮你更好管理XP系统!5款系统插件推荐