当前位置:   article > 正文

动态在JAVA里创建多个View并设置id

动态在JAVA里创建多个View并设置id

动态在JAVA里创建多个View并设置id

//定义放View的集合
private List<FrameLayout> mOldFrameLayout = new ArrayList<>();

//动态创建View
FrameLayout frameLayout = new FrameLayout(getContext());
//设置View的宽高
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
frameLayout.setLayoutParams(layoutParams);
//设置View的Id
frameLayout.setId(View.generateViewId());
//添加进集合
mOldFrameLayout.add(frameLayout);
//要想显示 最重要的一步 把最外层的布局 实例化后 添加对应的View
myLlParents.addView(frameLayout);
replaceFragment(mOldFrameLayout.get(i).getId(), new MyFragmentOne());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
 public void replaceFragment(int id,Fragment fragment) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(id,fragment);
        transaction.commit();
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/225304
推荐阅读
相关标签
  

闽ICP备14008679号