Dynamic TextView in Relative layout

前端 未结 3 1832
被撕碎了的回忆
被撕碎了的回忆 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.

    0 讨论(0)
  • 2020-12-06 11:56

    You may also add Dynamic textview to relative layout. Here with i have attached some code this may help you.

        RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
        for(int i = 0; i < 20; i++) 
        {
           TextView cb = new TextView(this);
           cb.setText("YORUMLAR"+yorum[0]+i);
           cb.setTextSize(30);
           cb.setId(2000+i);
           RelativeLayout.LayoutParams TextViewLayoutParams = new RelativeLayout.LayoutParams(100,100);
           if (i != 0 )DispViewLayoutParams.addRule(RelativeLayout.BELOW, 2000 - (i-1));
           ll.addView(cb,TextViewLayoutParams); 
        }
    
    0 讨论(0)
  • 2020-12-06 11:59

    You should use LinearLayout to automatically add one TextView after another.


    Assuming you can't live without RelativeLayout, you'll need to dynamically generate ids for all TextView you create in order to put one view under another. Here is example:

    public class HelloWorld extends Activity
    {       
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {       
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity);
    
            RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
    
            Random rnd = new Random();
            int prevTextViewId = 0;     
            for(int i = 0; i < 10; i++)
            {                       
                final TextView textView = new TextView(this);
                textView.setText("Text "+i);     
                textView.setTextColor(rnd.nextInt() | 0xff000000);            
    
                int curTextViewId = prevTextViewId + 1;
                textView.setId(curTextViewId);
                final RelativeLayout.LayoutParams params = 
                    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
                                                    RelativeLayout.LayoutParams.WRAP_CONTENT);
    
                params.addRule(RelativeLayout.BELOW, prevTextViewId);
                textView.setLayoutParams(params);
    
                prevTextViewId = curTextViewId;
                layout.addView(textView, params);
            }              
        }    
    }
    

    enter image description here

    0 讨论(0)
提交回复
热议问题