2011-05-07 113 views
25

我在這裏讀了這個地方,我完全失去了它,但可以使用一些幫助。動態添加textViews到linearLayout

我的應用程序將列名從sqlite拖到數組中。我想創建一個文本視圖併爲每個文本編輯文本(通過數組的大小),並且我記得在某處讀取可以像處理數組一樣處理textViews變量名稱,但我不知道現在在哪裏。

那麼,我將如何動態創建一個textView和editText爲無數列表是否在數組中?

它是像

TextView tv[] = new TextView()... 

for(...){ 
tv[i]... 
} 

這是正確的?

我感謝您的幫助!

+0

我想你會想一個ListView或類似的東西,而不是在textviews推... – jkhouw1 2011-05-07 00:37:36

+0

我也讀過,但我看到有一些問題ems其中人們無法從listView訪問editText。如果我確實設法將editText添加到列表中,我該如何動態獲取數據? – 2011-05-07 00:39:47

+0

ListView中存在'EditText'的問題。第一個是可以解決的焦點問題(請參閱http://stackoverflow.com/questions/2679948/focusable-edittext-inside-listview),但是如果您實際使用'EditText'作爲列表項而不是頁腳/頁眉那麼也存在視圖回收的問題 - 這意味着您需要在「EditText」文本離開屏幕並被回收之前保存文本。 – 2011-05-07 01:19:59

回答

56

類似下面的應該是你所需要的:

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; 
} 
+0

酷!請試試看,這也可以用於editText嗎? – 2011-05-07 01:20:40

+0

是的,相同的模式適用於'EditText',實際上任何視圖類型! – 2011-05-07 01:21:05

+0

約瑟夫,你真棒! – 2011-05-07 01:59:26

3

我認爲這將是有用的:

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); 
} 
7
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.. 
+2

如何將'OnClickListeners'綁定到創建的元素? – 2014-06-18 02:45:29

3

使用ArrayList的可以幫助你添加任何數量TextViews的動態。您甚至可能想要從父線性佈局中刪除特定的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)); 
0

因此,可以說你已經創建了一個的LinearLayout .xml文件,像這裏面:

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

現在的代碼中添加5個textviews動態

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 :) 
    } 
0

對我來說,這是一個解。

//設置變量

TextView t; 
ArrayList<TextView> textViewArrayList; 
LayoutInflater layoutInflater; 
LinearLayout ll_itensobrigatorios 

//告訴你的onCreate

layoutInflater = getLayoutInflater(); 
createViewItem(new String[]{"Fabio", "Santos", "Programador", "Natal"}); 

//這個創建視圖佈局

private void createViewItem(String[] nomes) { 
      textViewArrayList = new ArrayList<>(); 

      for(int i = 0; i < nomes.length; i++) { 
       View vll = layoutInflater.inflate(R.layout.nomes_tec_item, ll_itensobrigatorios, false); 
       t = (TextView) vll.findViewById(R.id.txt_tec_item); 
       textViewArrayList.add(t); 
       ll_itensobrigatorios.addView(vll); 
      } 

      for(int i = 0; i < textViewArrayList.size(); i++) { 
       textViewArrayList.get(i).setText((i + 1) + " - " + nomes[i]); 
      } 
} 
相關問題