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;

 

?[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 便可。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,动态布局是指在运行时根据特定条件或用户输入来动态地更改应用程序的布局。这种布局方式允许应用程序根据不同的屏幕尺寸、设备方向或其他因素来自适应地调整界面。 要实现动态布局,可以通过以下步骤进行操作: 1. 创建布局文件:首先,在res/layout文件夹中创建一个XML布局文件。这个文件将作为应用程序的初始布局。 2. 使用布局容器:在布局文件中使用适当的布局容器(如LinearLayout、RelativeLayout等)来容纳其他视图元素。 3. 设置视图属性:根据需要,在布局容器中添加视图元素(如按钮、文本框等),并为它们设置相应的属性,如宽度、高度、位置等。 4. 使用代码修改布局:在Java代码中,可以使用布局管理器来动态地更改布局。通过获取对布局容器的引用,可以添加、删除或修改其中的视图元素。 例如,可以使用以下代码将一个新的按钮添加到LinearLayout布局中: ```java LinearLayout layout = findViewById(R.id.myLinearLayout); // 获取对LinearLayout的引用 Button button = new Button(this); // 创建一个新的按钮 button.setText("动态按钮"); // 设置按钮文本 layout.addView(button); // 将按钮添加到LinearLayout中 ``` 通过这种方式,可以根据应用程序的需要动态地更改布局,以适应不同的条件或用户输入。 请注意,动态布局的实现还涉及到处理屏幕旋转、设备尺寸变化等情况时的布局更新和适配。在处理这些情况时,可以使用适配器模式、响应式布局等技术来简化开发过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值