Android动态布局

本文详细介绍了如何在相对布局(RelativeLayout)和线性布局(LinearLayout)中配置控件和按钮,包括添加按钮、设置ID、点击事件及布局参数等关键步骤。通过实例代码,帮助开发者更好地理解和应用Android布局设计。
摘要由CSDN通过智能技术生成

 

每次都忘记,记下来,以后方便查找,代码是从现在的项目中Copy出来的,先来个relativeLayout的

 
 
  1. private void setListPath(Context context, RelativeLayout footerRelativeLayout, String ButtonText) {
  2. if (flagAddFooterGuide) {
  3. // 我的音乐按钮
  4. ImageView myMusic = new ImageView(context);
  5. myMusic.setImageResource(R.drawable.style1_mymusic);
  6. myMusic.setOnClickListener(new OnClickListener() {
  7. @Override
  8. public void onClick(View v) {
  9. // TODO Auto-generated method stub
  10. ((MyMusicActivity) myContext).setSdCardFlag(false);
  11. ((MyMusicActivity) myContext).onCreate(null);
  12. }
  13. });
  14. myMusic.setId(1); // 设置ID为1
  15. ImageView myStorage = new ImageView(context);
  16. myStorage.setImageResource(R.drawable.style1_mymusic_usbstore);
  17. myStorage.setOnClickListener(new OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. // TODO Auto-generated method stub
  21. // 设置ListView为显示
  22. ((MyMusicActivity) myContext).setlistViewLinearLayoutVisible(false);
  23. // GalleryVisible为隐藏
  24. ((MyMusicActivity) myContext).setGalleryVisible(true);
  25. }
  26. });
  27. myStorage.setId(2); // 设置id为2
  28. // 设置本地当前目录的按钮
  29. MyImageButton myImageButtonFolderNow = new MyImageButton(context, R.drawable.style1_mymusic_details, ButtonText);
  30. RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
  31. ViewGroup.LayoutParams.WRAP_CONTENT);
  32. lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
  33. RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
  34. ViewGroup.LayoutParams.WRAP_CONTENT);
  35. lp2.addRule(RelativeLayout.RIGHT_OF, 1);//在控件ID为1的控件的右边
  36. RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
  37. ViewGroup.LayoutParams.WRAP_CONTENT);
  38. lp3.addRule(RelativeLayout.RIGHT_OF, 2);
  39. footerRelativeLayout.addView(myMusic, lp1);
  40. footerRelativeLayout.addView(myStorage, lp2);
  41. // 播放按钮
  42. MyImageButton myImageButtonPlay = new MyImageButton(context, R.drawable.mymusic_button_pause_backgroud, "播放");
  43. footerRelativeLayout.addView(myImageButtonFolderNow, lp3);
  44. myImageButtonPlay.setId(4);
  45. RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
  46. ViewGroup.LayoutParams.WRAP_CONTENT);
  47. lp4.addRule(RelativeLayout.LEFT_OF, 5);
  48. // 随机按钮
  49. MyImageButton myImageButtonPlayRandom = new MyImageButton(context, R.drawable.mymusic_button_play_random, "随机开");
  50. myImageButtonPlayRandom.setId(5);
  51. RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
  52. ViewGroup.LayoutParams.WRAP_CONTENT);
  53. lp5.addRule(RelativeLayout.LEFT_OF, 6);
  54. // 歌词按钮
  55. MyImageButton myImageButtonLyric = new MyImageButton(context, R.drawable.mymusic_button_lyric, "LRC歌词");
  56. myImageButtonLyric.setId(6);
  57. RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
  58. ViewGroup.LayoutParams.WRAP_CONTENT);
  59. lp6.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  60. // 加入
  61. footerRelativeLayout.addView(myImageButtonLyric, lp6);
  62. footerRelativeLayout.addView(myImageButtonPlayRandom, lp5);
  63. footerRelativeLayout.addView(myImageButtonPlay, lp4);
  64. flagAddFooterGuide = false;

 

 

再来个LinearLayout的

?[Copy to clipboard] Download zuiniuwang.java
  
  
  1. super.onCreate(icicle);
  2. LinearLayout layout = new LinearLayout(this);
  3. layout.setOrientation(LinearLayout.VERTICAL);
  4. btn = new Button(this);
  5. btn.setId(101);
  6. btn.setText("test looper");
  7. btn.setOnClickListener(this);
  8. LinearLayout.LayoutParams param =
  9. new LinearLayout.LayoutParams(100,50);
  10. param.topMargin = 10;
  11. layout.addView(btn, param);
  12. btn2 = new Button(this);
  13. btn2.setId(102);
  14. btn2.setText("exit");
  15. btn2.setOnClickListener(this);
  16. layout.addView(btn2, param);
  17. tv = new TextView(this);
  18. tv.setTextColor(Color.WHITE);
  19. tv.setText("");
  20. //其中 FP 是final int FP=LinearLayout.LayoutParams.FILL_PARENT;
  21. //final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
  22. LinearLayout.LayoutParams param2 =
  23. new LinearLayout.LayoutParams(FP, WC);
  24. param2.topMargin = 10;
  25. param2.setMargins(0, 0, 5, 0); //参数分别是(int left, int top, int right, int bottom)
  26. layout.addView(tv, param2);
  27. setContentView(layout);
 
一定要记住,创建的Parameter addview时,一定是这个子控件的参数,而不是这个容器本身的参数,这个相当重要,我在此处浪费了不少时间。如果需要对本Layout加入上参数可以 layout.setLayoutParams( XXX params) 和 layout.setXXX 便可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值