dynamic地将textView添加到linearLayout

我在这里读到这个地方,我完全失去了它,但可以使用一些援助。

我的应用程序是从SQLite的列名称拉到一个数组。 我想创build一个文本视图,并为每个文本编辑(通过数组的大小),我记得读过的地方,你可以把textViews的variables名称像一个数组,但我不知道现在在哪里。

那么,我将如何dynamic创build一个textView和editText为无数列表是在一个数组?

这是类似的东西

TextView tv[] = new TextView()... for(...){ tv[i]... } 

这是正确的吗?

我感谢您的帮助!

Solutions Collecting From Web of "dynamic地将textView添加到linearLayout"

像下面这样的东西应该是你所需要的:

 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 row #" + i); // add the textview to the linearlayout myLinearLayout.addView(rowTextView); // save a reference to the textview for later myTextViews[i] = rowTextView; } 
 LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout2); for (int i = 0; i < 5; i++) { TextView tv = new TextView(this); tv.setText("Dynamic TextView" + i); tv.setId(i + 5); ll.addView(tv); } u can do like this also.. 

我认为这将是有益的:

 int j = 0; context.getSystemService(Context.WINDOW_SERVICE); WindowManager manager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); for (int i = 0; i < tabsize; i++) { Tab tab = tabSet.get(i); if (i == selectedTabId) tab.setSelected(true); View view = tab.getView(); TableRow.LayoutParams pCol = new TableRow.LayoutParams(); pCol.width = display.getWidth() / tabSet.size(); rowBottom.addView(view, pCol); } 

使用ArrayList可以帮助您dynamic添加任意数量的TextView。 你甚至可能想从父线性布局中删除一个特定的TextView。 这是一种高效的记忆方式。 以下是一个片段。

 ArrayList<TextView> mTextViewList = new ArrayList<>(); //empty list of TextViews if(condition){ /* can repeat several times*/ //Create a temporary instance which will be added to the list final TextView mTextView = new TextView(this); //Add the instance to the ArrayList mTextViewList.add(mTextView); //Add view to the Parent layout in which you want to add your views mLinearLayout.addView(mTextView); } //Change the text of 6th(index:5) TextView which was added mTextViewList.get(5).setText("My Text"); //Remove 2nd(index:1) TextView from the parent LinearLayout mLinearLayout.removeView(mTextViewList.get(1)); 

所以可以说,你已经在.xml文件中创build了一个线性布局,如下所示:

 <LinearLayout android:orientation="vertical" android:id="@+id/linear" android:layout_width="match_parent" android:layout_height="wrap_content"> </LinearLayout> 

现在是dynamic添加5个textview的代码

  LinearLayout linearLayout= (LinearLayout)findViewById(R.id.linear); //find the linear layout linearLayout.removeAllViews(); //add this too for(int i=0; i<5;i++){ //looping to create 5 textviews TextView textView= new TextView(this); //dynamically create textview textView.setLayoutParams(new LinearLayout.LayoutParams( //select linearlayoutparam- set the width & height ViewGroup.LayoutParams.MATCH_PARENT, 48)); textView.setGravity(Gravity.CENTER_VERTICAL); //set the gravity too textView.setText("Textview: "+i); //adding text linearLayout.addView(textView); //inflating :) }