动态在TextViews下添加按钮

时间:2019-03-05 06:46:14

标签: android

我有以下内容:

    Button[] buttons = new Button[forSale.size()];
    TextView[] textViews = new TextView[forSale.size()];

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    for (int i = 0; i < forSale.size(); i++) {

        rl = (RelativeLayout)findViewById(R.id.marketView);

        textViews[i] = new TextView(this);
        textViews[i].setText("\n" + forSale.get(i).getTradeGoodType()
                + "\t$"
                + forSale.get(i).getTradeGoodType().getBasePrice() +
                "\n");
        textViews[i].setId(i);
        goodsForSaleText.append(textViews[i].getText());

        buttons[i] = new Button(getApplicationContext());
        buttons[i].setText("BUY");

        // add the rule that places your button below your TextView object
        params.addRule(RelativeLayout.BELOW, textViews[i].getId());

        buttons[i].setLayoutParams(params);
        rl.addView(buttons[i]);


    }

我的问题是我的按钮在屏幕顶部彼此叠加,而不是在TextViews下方彼此叠加。

基本上,它应该看起来像:

TextView

按钮

TextView

按钮

TextView

按钮

我需要为我拥有的每个TextView(从列表中派生)动态添加按钮。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

使用垂直方向的LinearLayout代替RelativeLayout。您将自动获得对象的垂直位置