How can we create dynamic textview?

前端 未结 6 1673
故里飘歌
故里飘歌 2021-01-21 20:13

how to create the textview in code not in xml file. It is because number of textviews will be changing in my application according to some integer.

相关标签:
6条回答
  • 2021-01-21 20:35

    You use TextView textView = new TextView(CurrentActivity.this);

    and then you add setter arguments that come with the TextView class

    0 讨论(0)
  • 2021-01-21 20:42
    private LinearLayout ll;
    private TextView tv;
    
    
          // in oncreate()
    onCreate()
    {
            int WrapWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
            int WrapHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
    
            tv = new TextView(this);
    
            ll.addView(tv,WrapWidth,WrapHeight);
    
    
    }
    
    0 讨论(0)
  • 2021-01-21 20:54

    Maybe thats what you need:

    LinearLayout lin = (LinearLayout) findViewById(R.id.myLinear);
    
      for (int i = 0; i <= 10 ; i++)
      {
          TextView myText = new TextView(this);
          myText.setText("textview# "+ i);
          lin.addView(myText);
      }
    
    0 讨论(0)
  • 2021-01-21 20:55

    Something like the following should be what you need:

    final int N = 10; // total number of textviews to add
    
    final TextView[] myTextViews = new TextView[N]; // create an empty array;
    
    for (int i = 0; i < N; i++) {
        // create a new textview
        final TextView rowTextView = new TextView(this);
    
        // set some properties of rowTextView or something
        rowTextView.setText("This is TextView #" + i);
    
        // add the textview to the linearlayout
        myLinearLayout.addView(rowTextView);
    
        // save a reference to the textview for later
        myTextViews[i] = rowTextView;
    }
    
    0 讨论(0)
  • 2021-01-21 20:57

    Code is here

    final int c = 12;
    final TextView[] mtext = new TextView[c]; 
    for (int i = 0; i < c; i++) {
    TextView rowtxt = new TextView(this);
    rowtxt.setText("Hello" + i);
    myLinearLayout.addView(rowtxt);
    myTextViews[i] = rowtxt;
    myTextViews[i].setOnClickListener(onclicklistener);//textview click
    

    }

    OnClickListeners code is here

    OnClickListener onclicklistener = new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v == myTextViews[0]){
            //do whatever you want....
        }
    }
    

    };

    Hope it is helpful for you

    0 讨论(0)
  • 2021-01-21 20:59

    This is the code to create TextView Dynamically

    LinearLayout layout = (LinearLayout ) findViewById(R.id.llayout);
    
    for (int i = 0; i < 3; i++) {
    
    TextView dynamicTextView = new TextView(this);
    dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            dynamicTextView.setText("NewYork");
            layout.addView(tstate);
    
    }
    
    0 讨论(0)
提交回复
热议问题