Programmatic RelativeLayout
I'm attempting to programmatically add several Tiles which extend from TextViews
into a RelativeLayout
.
My code is as follows.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < characters.length; i++) {
Tile tile = new Tile(this);
tile.setText(Character.toString(characters[i]));
tile.setId(i);
if (i != 0) {
params.addRule(RelativeL开发者_JS百科ayout.RIGHT_OF, i - 1);
}
_display.addView(tile, params);
}
So I'm creating a new instance class of LayoutParams
called params
and adding a rule to align each tile to the right of the previous tile. When I run the app it appears that the tiles are overlapping over each other. Any suggestions?
You can't reuse the same LayoutParams object for every view. You need to create a new one every time.
精彩评论