>
我通过资源为字符串动态显示 13张卡并在布局中设置其边距,但他们没有设置而不重叠。我通过更改保证金尝试了很多但没有工作。
int counter=0;
forloop 1 to 13
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
im = (ImageView) findViewById(resID);
Context context = im.getContext();
cardID = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(im.getLayoutParams());
lp.setMargins(counter*30,0,0,0);//left,right,top,bottom
im.setLayoutParams(lp);
im.setImageResource(cardID);
counter++;
![Screen shot of 13 Cards ][2]
答案 0 :(得分:0)
问题是,一旦你的水平空间不足,你需要创建一个新行并添加卡片,直到该行用完空间等为止。
GridLayout更适合您尝试完成的任务。您还可以使用GridView或TableLayout。使用RelativeLayout这样做会相当复杂,因为子视图需要相对于彼此进行布局,因此您需要确切地知道每个孩子在哪个视图的右侧以及哪个视图哪个视图&#39在下面。
有关如何动态地向GridLayout添加视图的示例,请参阅本教程: http://android-er.blogspot.com/2014/09/insert-view-to-gridlayout-dynamically.html