Android動態佈局


 

每次都忘記,記下來,以後方便查找,代碼是從現在的項目中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 便可。
發佈了45 篇原創文章 · 獲贊 3 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章