开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜