2014-10-07 47 views
0

我以编程方式设置我的布局,因为我填充它取决于我从请求中获得多少图像。我想要做的是在图片右侧创建两个textview,第一个在第一个下面。随着我在其他答案中发现的,我结束了我的第二个textview是重叠的图像,并不低于其他textview。使textview在另一个textview下,并以编程方式在图像的右侧

int counter = 0; 
    while(mList.size()!=counter) 
    { 
     RelativeLayout row = new RelativeLayout(context); 
     row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));    

     ImageButton imgBtn = new ImageButton(context); 
     Bitmap image = getImage(mList.get(counter)); 
     imgBtn.setImageBitmap(image); 
     imgBtn.setId(counter + 1); // Because it need to be a positive integer. 
     imgBtn.setBackground(null); 
     imgBtn.setPadding(0, 0, 0, 0); 
     row.addView(imgBtn); 

     TextView firstText = new TextView(context); 
     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     lp.addRule(RelativeLayout.RIGHT_OF, imgBtn.getId()); 
     lp.addRule(RelativeLayout.CENTER_IN_PARENT); 
     firstText.setLayoutParams(lp); 
     firstText.setId(counter+100); 
     row.addView(firstText); 

     TextView secondText = new TextView(context); 
     RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     lp2.addRule(RelativeLayout.BELOW,firstText.getId()); 
     secondText.setLayoutParams(lp2); 
     row.addView(secondText); 

     ++counter; 
     linlayout.addView(row); 
    } 

回答

1

使用linearLayout而不是relative.In在这里你不需要设置counter和id。将setTag替换为视图。使代码更具可读性。

您可以设置行布局的方向,然后不需要担心其他参数来定位视图。

我不添加代码因为我知道你可以写得很好,只是改变方法。

+0

确实,我没有任何重叠,但textView不是相对于像我想要的图像,两个textView只是一个接一个。有没有办法使用LinearLayout将第二个textview放置在第一个图像的下方并且仍然在图像的右侧? – ggc 2014-10-07 19:06:46

+0

使用您的父母作为LinearLayout L1,水平方向,然后将图像添加到它(L1)。再次使用垂直方向的LinearLayout(L2),将两个textView添加到它(L2),然后将L2添加到L1,您将得到结果 – 2014-10-07 19:14:00

+0

嗯,听起来是对的。尝试它 – ggc 2014-10-07 19:25:32

相关问题