在相对布局中以编程方式添加视图

我想这样编程:

view1 | view2 view3 | view4 ---------------- view1 | view2 view3 | view4 ---------------- view1 | view2 view3 | view4 --------------- ........... ...... which keeps repeating -------------- 

我不想使用ListView。

3 Solutions collect form web for “在相对布局中以编程方式添加视图”

重要提示:请记住为每个视图设置ID。

 RelativeLayout layout = new RelativeLayout(this); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layout.setLayoutParams(layoutParams); RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); TextView tv1 = new TextView(this); tv1.setId(1); tv1.setText("textView1"); TextView tv2 = new TextView(this); params2.addRule(RelativeLayout.RIGHT_OF, tv1.getId()); tv2.setId(2); tv2.setText("textView2"); TextView tv3 = new TextView(this); params3.addRule(RelativeLayout.BELOW, tv1.getId()); tv3.setId(3); tv3.setText("textView3"); TextView tv4 = new TextView(this); params4.addRule(RelativeLayout.RIGHT_OF, tv3.getId()); params4.addRule(RelativeLayout.ALIGN_BOTTOM, tv3.getId()); tv4.setId(4); tv4.setText("textView4"); layout.addView(tv1, params1); layout.addView(tv2, params2); layout.addView(tv3, params3); layout.addView(tv4, params4); 

在@Askilondz答案中build议使用多个布局参数,而removeRule像下面那样使用addRuleremoveRule

 RelativeLayout rl = new RelativeLayout(this.getContext()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); TextView tv1 = new TextView(this); tv1.setId(View.generateViewId()); tv1.setText("textView1"); TextView tv2 = new TextView(this); tv2.setId(View.generateViewId()); tv2.setText("textView2"); lp.addRule(RelativeLayout.BELOW, tv1.getId()); rl.addView(tv2, lp); lp.removeRule(RelativeLayout.BELOW); . . . 

如果使用SDK <17,则必须创buildid's.xml文件,如values -> ids.xml ,其结构如下所示:

 <?xml version="1.0" encoding="utf-8"?> <resources> <item name="tv1" type="id" /> <item name="tv2" type="id" /> </resources> 

并将它们设置为您的代码:

 tv1.setId(R.id.tv1); tv2.setId(R.id.tv2); 

更新它看起来相同的LayoutParams添加多个规则不能正常工作,无论如何,我会保持我的答案,以防有人罚款对他有用的东西

 ImageView imageView = new ImageView(this); // set the Drawable on the ImageView imageView.setImageDrawable(bmd); // center the Image imageView.setScaleType(ScaleType.CENTER); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
  • 将一个库/ JAR添加到Eclipse Android项目中
  • 如何从Java Android平台的外部IP
  • “更喜欢在Kotlin上运行匕首处理器”
  • 我怎样才能从地址找到经纬度?
  • 在android studio中由colorpid彩色logcat
  • 您关注哪些Android开发博客
  • onCreate()和onCreateView()会调用比需要更多的(Fragments)
  • 即使使用正确的api密钥,Google地图API请求也会被拒绝进行Android自动填充
  • RecyclerView中最后一个子项的边距/填充
  • 以编程方式在android中设置“布局到右侧”属性
  • canvas.translate是做什么的?