在 Android 中在屏幕上设置 13 张卡片
Setting 13 cards on screen in Android
>
我通过获取 字符串形式的资源 并在布局中设置边距来动态显示 13 张卡片,但它们没有设置 没有重叠。我通过更改 margin 尝试了很多但没有用。
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]
问题是一旦 运行 超出水平 space,您需要创建一个新行并向其中添加卡片,直到该行 运行 超过 [=17] =],等等
GridLayout 更适合您要实现的目标。您还可以使用 GridView 或 TableLayout。使用 RelativeLayout 执行此操作会相当复杂,因为子视图需要相对于彼此进行布局,因此您需要确切地知道每个子视图在哪个视图的右边以及哪个视图在下面。
有关如何动态向 GridLayout 添加视图的示例,请参阅本教程:
http://android-er.blogspot.com/2014/09/insert-view-to-gridlayout-dynamically.html
我通过获取 字符串形式的资源 并在布局中设置边距来动态显示 13 张卡片,但它们没有设置 没有重叠。我通过更改 margin 尝试了很多但没有用。
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]
问题是一旦 运行 超出水平 space,您需要创建一个新行并向其中添加卡片,直到该行 运行 超过 [=17] =],等等
GridLayout 更适合您要实现的目标。您还可以使用 GridView 或 TableLayout。使用 RelativeLayout 执行此操作会相当复杂,因为子视图需要相对于彼此进行布局,因此您需要确切地知道每个子视图在哪个视图的右边以及哪个视图在下面。
有关如何动态向 GridLayout 添加视图的示例,请参阅本教程: http://android-er.blogspot.com/2014/09/insert-view-to-gridlayout-dynamically.html