Android中利用LinearLayout动态添加控件
2010-03-26 20:19
656 查看
在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派生的类有:
Known Indirect Subclasses
AbstractInputMethodService
,
Activity
,
ActivityGroup
,
AliasActivity
,
Application
,
ContextThemeWrapper
,
ExpandableListActivity
,
InputMethodService
,
IntentService
,
IsolatedContext
,
LauncherActivity
,
ListActivity
,
MockApplication
,
MutableContextWrapper
,
PreferenceActivity
,
RenamingDelegatingContext
,
Service
,
TabActivity
1.动态添加2个垂直排列的Button
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); final LinearLayout layout2=new LinearLayout(this); layout2.setOrientation(LinearLayout.VERTICAL); Button btn1=new Button(this); setContentView(layout2); Button btn2=new Button(this); btn1.setText("Button1"); btn2.setText("Button2"); layout2.addView(btn1); layout2.addView(btn2); setContentView(layout2); }
final LinearLayout layout2=new LinearLayou(this);
定义一个LinearLayout ,参数为context在这儿即为this
layout2.setOrientation(LinearLayout.VERTICAL);
设置layout格式为vertical,竖直排列
2.在Button的click事件中动态添加一个button
OnClickListener listen1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); final LinearLayout layout2=new LinearLayout(this); layout2.setOrientation(LinearLayout.VERTICAL); Button btn1=new Button(this); setContentView(layout2); Button btn2=new Button(this); btn1.setText("Button1"); btn2.setText("Button2"); layout2.addView(btn1); layout2.addView(btn2); listen1 = new OnClickListener() { public void onClick(View v) { setTitle("点击button1 "); Button btn3=new Button(v.getContext()); layout2.addView(btn3); btn3.setText("Button3"); } }; btn1.setOnClickListener(listen1); }
与上一例子区别就是,在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动态添加UI控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- android动态加载控件(LinearLayout)并获取动态添加的EditText里的值
- Android 动态添加布局(layout)和控件(Widget)
- android 给LinearLayout中添加一定数量的控件,并让着一定数量的控件从右到左移动,每隔若干秒停顿一下,最后一个view链接第一个view,然后继续移动循环往复,形成一个死循环简单动画效果
- android LinearLayout 动态添加子View
- Android 搜索页-搜索历史条目动态添加(LinearLayout动态添加条目)
- 【学习笔记】Android 动态添加控件 getlayoutparams 为空的问题
- LinearLayout动态添加控件及将某XML动态加入到Activity显示(1)
- Android Linearlayout中动态添加ImageView
- android,利用layoutParams代码动态布局空间位置
- android 动态调整RelativeLayout布局控件的相对位置 layout_toLeftOf 之类的
- Android 布局之LinearLayout 子控件weight权重的作用详析
- android动态添加删除控件以及设置宽高
- Android布局控件之LinearLayout
- android LinearLayout添加分隔线
- Android布局控件之LinearLayout详解