Android动态布局

 2023-09-05 阅读 254 评论 0

摘要:每次都忘记,记下来,以后方便查找,代码是从现在的项目中Copy出来的,先来个relativeLayout的 private void setListPath(Context context, RelativeLayout footerRelativeLayout, String ButtonText) { if (flagAddFooterGuide) { // 我的音乐按钮

 

每次都忘记,记下来,以后方便查找,代码是从现在的项目中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;

 

?[Copy to clipboard]Download zuiniuwang.java

再来个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 便可。


本文转自 最牛傻蛋 51CTO博客,原文链接:http://blog.51cto.com/zuiniuwang/720070,如需转载请自行联系原作者

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/175.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息