gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:59:10 25 4
gpt4 key购买 nike

我知道可以向 LinearLayout 添加一些布局,例如 ButtonTextView。我想根据 for 循环的条件创建多个 TextView

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

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

我已经设置好布局

setContentView(R.layout.result_page);

我正在使用 Function 将该 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 的代码得到的结果是这样的:
enter image description here

但是我想在tag word下设置Word,在tag 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);
}

关于android - 如何动态添加多个TextView到main.xml中定义的LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7334751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com