如何动态添加多个TextView到main.xml中定义LinearLayout

时间:2011-09-07 13:34:39

标签: android android-layout android-emulator android-widget android-ndk

我知道可以向Button添加TextViewLinearLayout等布局。我想根据for循环的条件创建多个TextView

我以自己的方式尝试过,但我无法创建它。有人知道如何创建吗?

for循环将根据条件而有所不同。请帮我解决这个问题。是否可以创建TextView

的数组

我已经用

设置了布局
setContentView(R.layout.result_page);

我正在使用Function将该视图添加到现有视图中:

功能是:

public void addAll()
{
     LinearLayout layout = (LinearLayout)findViewById(R.id.myLayout);     
     layout.setOrientation(1);       
     TextView name[] = null;     
     TextView website[] = null;     
     TextView category[] = null;        
     for (int i = 0; i < 5; i++) 
     {         
         name[i] = new TextView(this);         
         name[i].setText("Name = Shreyash");         
         website[i] = new TextView(this);         
         website[i].setText("Website = shreyah.co.cc");         
         category[i] = new TextView(this);         
         category[i].setText("Website Category = OWN");         
         layout.addView(name[i]);         
         layout.addView(website[i]);         
         layout.addView(category[i]);     
    } 
}

但之后如果我运行应用程序,它会显示错误,如:

09-08 11:03:28.755: ERROR/AndroidRuntime(318): FATAL EXCEPTION: main
09-08 11:03:28.755: ERROR/AndroidRuntime(318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quiz.spellingquiz/com.quiz.spellingquiz.ResultDisplayPage}: java.lang.NullPointerException
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at android.os.Looper.loop(Looper.java:123)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at java.lang.reflect.Method.invoke(Method.java:521)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at dalvik.system.NativeStart.main(Native Method)
09-08 11:03:28.755: ERROR/AndroidRuntime(318): Caused by: java.lang.NullPointerException
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at com.quiz.spellingquiz.ResultDisplayPage.addAll(ResultDisplayPage.java:59)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at com.quiz.spellingquiz.ResultDisplayPage.onCreate(ResultDisplayPage.java:34)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-08 11:03:28.755: ERROR/AndroidRuntime(318):     ... 11 more

我对此错在哪里?

我用DeeV的代码得到了这样的结果:
enter image description here

但是我想在标签词下设置Word,并在标签答案下设置答案。但是怎么可能呢? 我想我必须为此设置另一个LinearLayout。我如何为这种外观设置另一个LinearLayout?

2 个答案:

答案 0 :(得分:11)

如果您稍后需要更改TextViews,则只需创建一个数组。但是,如果你确实需要创建一个数组,那么这样的东西应该可以工作。

List<TextView> textList = new ArrayList<TextView>(NUM_OF_TEXTS);
for(int i = 0; i < NUM_OF_TEXTS; i++)
{
   TextView newTV = new TextView(context);
   newTV.setText("New message.");
   newTV.setTextColor(0xFFFF0000);
   /**** Any other text view setup code ****/
   myLinearLayout.addView(newTV);
   textList.add(newTV);
}

如果文本在创建后是静态的,那么您只需删除代码中对列表的任何引用,它仍会添加到LinearLayout

修改

假设我理解你的问题,你希望布局是这样的:

Word:
Big
Answer:
42

Word:
Small
Answer:
Tough

Word:
Example
Answer:
Another Answer

在这种情况下,你实际上不需要做太多。 LinearLayout会将所有内容按照addView放置的顺序排列。要更新我之前的代码,这应该有效:

List<TextView> wordList = new ArrayList<TextView>(NUM_OF_WORDS);
List<TextView> answerList = new ArrayList<TextView>(NUM_OF_ANSWERS);

for(int i = 0; i < NUM_OF_WORDS; i++){
   TextView blankText = new TextView(context);
   TextView wordText = new TextView(context);
   TextView answerText = new TextView(context);
   blankText.setText(" ");
   wordText.setText("Word:");
   answerText.setText("Answer:");

   TextView newWord = new TextView(context);
   newWord.setText(**** some method of getting the word ****);
   TextView newAnswer = new TextView(context);
   newAnswer.setText(**** some method of getting the answer ****);
   /**** Any other text view setup code ****/

   myLinearLayout.addView(wordText);
   myLinearLayout.addView(newWord);
   myLinearLayout.addView(answerText);
   myLinearLayout.addView(newAnswer);
   myLinearLayout.addView(blankText);

   wordList.add(newWord);
   answerList.add(newAnswer);
}

答案 1 :(得分:2)