Dynamic TextView in Relative layout

前端 未结 3 1836
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 11:34

I am triying to use dynamic layout for comment part of my project but when i settext of textview dynamicly the output only appears in top of the screen. And it puts the outp

3条回答
  •  伪装坚强ぢ
    2020-12-06 11:45

    You've to provide the location of your newly added view. As @Adinia said, with no position, it will be aligned to the top by default. So you can use the following code to do it with RelativeLayout;

    RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);
    
    for (int i = 0; i < 20; i++) {
    
        TextView dynaText = new TextView(this);
    
        dynaText.setText("Some text " + i);
        dynaText.setTextSize(30);
    
        // Set the location of your textView.
        dynaText.setPadding(0, (i * 30), 0, 0);
    
        containerLayout.addView(dynaText);
    }
    

    If you want to show multiple textviews one after the other, then you should go with LinearLayout.

提交回复
热议问题