我知道这是可能的一些布局,像Button
和TextView
添加到LinearLayout
。我想基于for循环的条件,创造更多然后一个TextView
。
我有我自己的方式尝试过,但我不能来创建它。有谁知道如何创建呢?
for循环将根据情况有所不同。请帮我对此。是否有可能创造TextView
的阵列?
我已经设置与布局
setContentView(R.layout.result_page);
我使用的功能到该视图添加到现有的观点:
该功能是:
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和标签回答下答案。但它是如何可能的?我觉得我有另设的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);
}
LinearLayout lila = new LinearLayout(this);
ArrayList<Button> alb = new ArrayList<Button>();
int nButton = 10;
for (int i = 0; i < nButton; i++)
{
alb.add(new Button(this));
lila.addView(alb.get(i));
}
//works the same way with TextView
alb.get(5).setText("myButton");
也许这可以帮助。
编辑:对不起严格一样DeeV。