java代码动态网布局里面添加控件
2013-11-28 18:30
190 查看
private void setListPath(Context context, RelativeLayout footerRelativeLayout, String ButtonText) { if (flagAddFooterGuide) { // 我的音乐按钮 ImageView myMusic = new ImageView(context); myMusic.setImageResource(R.drawable.style1_mymusic); myMusic.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ((MyMusicActivity) myContext).setSdCardFlag(false); ((MyMusicActivity) myContext).onCreate(null); } }); myMusic.setId(1); // 设置ID为1 ImageView myStorage = new ImageView(context); myStorage.setImageResource(R.drawable.style1_mymusic_usbstore); myStorage.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 设置ListView为显示 ((MyMusicActivity) myContext).setlistViewLinearLayoutVisible(false); // GalleryVisible为隐藏 ((MyMusicActivity) myContext).setGalleryVisible(true); } }); myStorage.setId(2); // 设置id为2 // 设置本地当前目录的按钮 MyImageButton myImageButtonFolderNow = new MyImageButton(context, R.drawable.style1_mymusic_details, ButtonText); RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT); RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp2.addRule(RelativeLayout.RIGHT_OF, 1);//在控件ID为1的控件的右边 RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp3.addRule(RelativeLayout.RIGHT_OF, 2); footerRelativeLayout.addView(myMusic, lp1); footerRelativeLayout.addView(myStorage, lp2); // 播放按钮 MyImageButton myImageButtonPlay = new MyImageButton(context, R.drawable.mymusic_button_pause_backgroud, "播放"); footerRelativeLayout.addView(myImageButtonFolderNow, lp3); myImageButtonPlay.setId(4); RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp4.addRule(RelativeLayout.LEFT_OF, 5); // 随机按钮 MyImageButton myImageButtonPlayRandom = new MyImageButton(context, R.drawable.mymusic_button_play_random, "随机开"); myImageButtonPlayRandom.setId(5); RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp5.addRule(RelativeLayout.LEFT_OF, 6); // 歌词按钮 MyImageButton myImageButtonLyric = new MyImageButton(context, R.drawable.mymusic_button_lyric, "LRC歌词"); myImageButtonLyric.setId(6); RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp6.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // 加入 footerRelativeLayout.addView(myImageButtonLyric, lp6); footerRelativeLayout.addView(myImageButtonPlayRandom, lp5); footerRelativeLayout.addView(myImageButtonPlay, lp4); flagAddFooterGuide = false;
案例:
LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); btn = new Button(this); btn.setId(101); btn.setText("test looper"); btn.setOnClickListener(this); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100,50); param.topMargin = 10; layout.addView(btn, param); btn2 = new Button(this); btn2.setId(102); btn2.setText("exit"); btn2.setOnClickListener(this); layout.addView(btn2, param); tv = new TextView(this); tv.setTextColor(Color.WHITE); tv.setText(""); //其中 FP 是final int FP=LinearLayout.LayoutParams.FILL_PARENT; //final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC); param2.topMargin = 10; param2.setMargins(0, 0, 5, 0); //参数分别是(int left, int top, int right, int bottom) layout.addView(tv, param2); setContentView(layout);
相关文章推荐
- 请问Android中怎么在一个线性布局里面添加多个图表控件
- 请问Android中怎么在一个线性布局里面添加多个图表控件
- 动态添加设置布局与控件
- 一步步学习微软InfoPath2010和SP2010--第三章节--表单设计基础:处理InfoPath布局、控件和视图(7)--添加含规则的提交按钮到Flight Delay表单
- VC++ MFC ListBox里面添加按钮等控件
- 在listview的前面或后面添加布局控件
- QScrollArea里面添加控件的时候,自动滚到到最后
- 布局添加控件问题——addView
- Android根据布局文件动态添加控件
- Android 在布局容器中动态添加控件
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- VS2005出现无法在DialogEditor里面为控件添加变量和事件句柄
- Android 在程序中动态添加 View 布局或控件
- [Android_Develop]在代码中实时添加、移除布局、控件
- 计划在控件中添加布局属性
- 后台动态添加布局文件、控件与动态设置属性
- Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标
- 动态添加综合布局---动态添加控件及将某XML动态加入到Activity显示(续)
- vs里面添加DevExpress控件
- 用textView 画圆 更换颜色 动态添加到布局里面