我知道可以在Button中添加一些布局,例如TextViewLinearLayout。我想根据for循环的条件创建一个以上的TextView

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

for循环将根据条件而有所不同。请帮助我。是否可以创建TextView数组?

我已经用

setContentView(R.layout.result_page);

我正在使用功能将该 View 添加到我现有的 View 中:

函数为:
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的代码的结果:

但是我想将Word设置在标签word下,并将Answer设置在标签answer下。但是怎么可能呢?
我想我必须为此设置另一个LinearLayout。如何为该外观类型设置另一个LinearLayout?

最佳答案

仅当以后需要更改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);
}

10-07 19:25
查看更多