动态文本视图不适合多行

发布于 2025-01-08 07:58:00 字数 6232 浏览 1 评论 0原文

我的 Android 应用程序出现问题。我想在 TableLayout 中动态创建 4 个 TableRow。该行的每一行将包含 1 个 TextView。我想要这样的 TextView:

 textview1
 textview2
 textview3
 textview4

在 4 个不同的行中。

我能够动态创建 Tewtview 但大小不匹配。例如,如果我的第一个文本视图包含大文本,它似乎是单个文本,而不会继续到下一行,

我读了很多类似的问题,但找不到解决方案。这是我的布局文件:

<Relativelayout>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="10dip" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="@+id/didyoudo"
android:layout_above="@+id/bardown" android:scrollbarFadeDuration="1000"
android:scrollbarSize="12dip">

<TableLayout android:id="@+id/answertable"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:stretchColumns="0,1" android:collapseColumns="2">


  </TableLayout>
</ScrollView>
<Relativelayout/>

这就是我动态生成行和文本视图的方式

for (k =0; k< questionCount;k++ ) {
                    //tmpDict = trivialist.get(k);
                    Trivia trivia = trivialist.get(k);
                    TableRow row = new TableRow(getApplicationContext());
                    TableRow row1 = new TableRow(getApplicationContext());
                    TableRow row2 = new TableRow(getApplicationContext());
                    TableRow row3 = new TableRow(getApplicationContext());
                    TextView tv1 = new TextView(getApplicationContext());
                    TextView tv2 = new TextView(getApplicationContext());
                    TextView tv3 = new TextView(getApplicationContext());
                    TextView tv4 = new TextView(getApplicationContext());
                     desc = trivia.getAnswerDesc();
                     quizquestion = trivia.getStrQuestion();
                    tv1.setText("\n\t" + quizquestion);
                    tv1.setTextColor(Color.WHITE); 
                    tv1.setSingleLine(false);
                    tv1.setLines(2);
                    tv1.setHorizontallyScrolling(false);
                    tv1.setTextSize(15);
                    tv1.setTypeface(null, Typeface.BOLD);

                            try {
                                //if the below value is null, it means that the selected answer is wrong 
                                // and enters to the else block
                                if (triviaDict.get("correctAnswer") != null) {
                                    String answer = (String) triviaDict.get("correctAnswer");
                                    tv2.setText("\n\t" + "You said: "+ answer + "\n");
                                    tv2.setTextColor(Color.WHITE);
                                    tv2.setSingleLine(false);
                                    tv2.setHorizontallyScrolling(false);
                                    tv2.setTextSize(15);
                                    tv2.setLines(2);
                                    tv2.setTypeface(null, Typeface.BOLD);

                                    tv3.setText("\t" +"That's Right."+ desc+"\n");
                                    tv3.setTextColor(Color.WHITE);
                                    tv3.setSingleLine(false);
                                    tv3.setHorizontallyScrolling(false);
                                    tv3.setTextSize(15);
                                    tv3.setLines(2);
                                    tv3.setTypeface(null, Typeface.BOLD);

                                    tv4.setText("\t" + "_____________________________________" );
                                    tv4.setTextColor(Color.WHITE);
                                    tv3.setHorizontallyScrolling(false);
                                    tv4.setTypeface(null, Typeface.BOLD);
                                } else {
                                    String wronganswer = (String) triviaDict.get("selected");
                                    tv2.setText("\n\t" + "You said:"+ wronganswer +"\n" );
                                    tv2.setTextColor(Color.WHITE);
                                    tv2.setTextSize(15);
                                    tv2.setLines(2);
                                    tv2.setSingleLine(false);
                                    tv2.setTypeface(null, Typeface.BOLD);

                                    tv3.setText("\t" + "Actually," + desc +"\n");
                                    tv3.setTextColor(Color.WHITE);
                                    tv3.setTextSize(15);
                                    tv3.setLines(2);
                                    tv3.setSingleLine(false);
                                    tv3.setTypeface(null, Typeface.BOLD);

                                    tv4.setText("\t" + "_____________________________________" );
                                    tv4.setTextColor(Color.WHITE);
                                    tv3.setHorizontallyScrolling(false);
                                    tv4.setTypeface(null, Typeface.BOLD);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }


                tv1.setLayoutParams(new TableRow.LayoutParams(0,
                                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));
                tv2.setLayoutParams(new TableRow.LayoutParams(0,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));
                tv3.setLayoutParams(new TableRow.LayoutParams(0,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));


                    row.addView(tv1);
                    row1.addView(tv2);
                    row2.addView(tv3);
                    row3.addView(tv4);
                    answerTable.addView(row);
                    answerTable.addView(row1);
                    answerTable.addView(row2);
                    answerTable.addView(row3);

                }

,但没有什么是完美的。这适用于第三个文本视图,但不适用于第一个文本视图。不知道发生了什么。

谁能告诉我该怎么办。

更新

我对这段代码没有任何问题,除了只有 tv3 会继续到大文本的下一行,而 tv1 不会。 我希望 tv1、tv2 和 tv3 全部在下一行继续,以防文本较大

I'm having an issue with my android application. I want to dynamically create 4 TableRows in a TableLayout. Each of this row will contain 1 TextView. I want the TextView like this:

 textview1
 textview2
 textview3
 textview4

in 4 different rows.

I am able to create the Tewtview dynamically but the sizes are not matching. For example if my first textview contains a big text, it appears to be in single text without being continued to the next line

I read a lot of similar questions but couldn't find the solution. Here is my layout file:

<Relativelayout>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="10dip" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="@+id/didyoudo"
android:layout_above="@+id/bardown" android:scrollbarFadeDuration="1000"
android:scrollbarSize="12dip">

<TableLayout android:id="@+id/answertable"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:stretchColumns="0,1" android:collapseColumns="2">


  </TableLayout>
</ScrollView>
<Relativelayout/>

And this is how I'm generating the rows and textview dynamically

for (k =0; k< questionCount;k++ ) {
                    //tmpDict = trivialist.get(k);
                    Trivia trivia = trivialist.get(k);
                    TableRow row = new TableRow(getApplicationContext());
                    TableRow row1 = new TableRow(getApplicationContext());
                    TableRow row2 = new TableRow(getApplicationContext());
                    TableRow row3 = new TableRow(getApplicationContext());
                    TextView tv1 = new TextView(getApplicationContext());
                    TextView tv2 = new TextView(getApplicationContext());
                    TextView tv3 = new TextView(getApplicationContext());
                    TextView tv4 = new TextView(getApplicationContext());
                     desc = trivia.getAnswerDesc();
                     quizquestion = trivia.getStrQuestion();
                    tv1.setText("\n\t" + quizquestion);
                    tv1.setTextColor(Color.WHITE); 
                    tv1.setSingleLine(false);
                    tv1.setLines(2);
                    tv1.setHorizontallyScrolling(false);
                    tv1.setTextSize(15);
                    tv1.setTypeface(null, Typeface.BOLD);

                            try {
                                //if the below value is null, it means that the selected answer is wrong 
                                // and enters to the else block
                                if (triviaDict.get("correctAnswer") != null) {
                                    String answer = (String) triviaDict.get("correctAnswer");
                                    tv2.setText("\n\t" + "You said: "+ answer + "\n");
                                    tv2.setTextColor(Color.WHITE);
                                    tv2.setSingleLine(false);
                                    tv2.setHorizontallyScrolling(false);
                                    tv2.setTextSize(15);
                                    tv2.setLines(2);
                                    tv2.setTypeface(null, Typeface.BOLD);

                                    tv3.setText("\t" +"That's Right."+ desc+"\n");
                                    tv3.setTextColor(Color.WHITE);
                                    tv3.setSingleLine(false);
                                    tv3.setHorizontallyScrolling(false);
                                    tv3.setTextSize(15);
                                    tv3.setLines(2);
                                    tv3.setTypeface(null, Typeface.BOLD);

                                    tv4.setText("\t" + "_____________________________________" );
                                    tv4.setTextColor(Color.WHITE);
                                    tv3.setHorizontallyScrolling(false);
                                    tv4.setTypeface(null, Typeface.BOLD);
                                } else {
                                    String wronganswer = (String) triviaDict.get("selected");
                                    tv2.setText("\n\t" + "You said:"+ wronganswer +"\n" );
                                    tv2.setTextColor(Color.WHITE);
                                    tv2.setTextSize(15);
                                    tv2.setLines(2);
                                    tv2.setSingleLine(false);
                                    tv2.setTypeface(null, Typeface.BOLD);

                                    tv3.setText("\t" + "Actually," + desc +"\n");
                                    tv3.setTextColor(Color.WHITE);
                                    tv3.setTextSize(15);
                                    tv3.setLines(2);
                                    tv3.setSingleLine(false);
                                    tv3.setTypeface(null, Typeface.BOLD);

                                    tv4.setText("\t" + "_____________________________________" );
                                    tv4.setTextColor(Color.WHITE);
                                    tv3.setHorizontallyScrolling(false);
                                    tv4.setTypeface(null, Typeface.BOLD);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }


                tv1.setLayoutParams(new TableRow.LayoutParams(0,
                                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));
                tv2.setLayoutParams(new TableRow.LayoutParams(0,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));
                tv3.setLayoutParams(new TableRow.LayoutParams(0,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));


                    row.addView(tv1);
                    row1.addView(tv2);
                    row2.addView(tv3);
                    row3.addView(tv4);
                    answerTable.addView(row);
                    answerTable.addView(row1);
                    answerTable.addView(row2);
                    answerTable.addView(row3);

                }

But nothing is working perfect. This works for the 3rd textview but not for the 1st one. Don't know what is happening.

Can anybody tell me what to do.

Update

I do not have any problem with this code except that only tv3 gets proceeded to the next line on large text but not the tv1.
I want tv1, tv2 and tv3 all to be continued in the next line in case of large text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情 2025-01-15 07:58:00
    <Relativelayout>

<ScrollView 
android:layout_marginTop="10dip" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="@+id/didyoudo"
android:layout_above="@+id/bardown" android:scrollbarFadeDuration="1000"
android:scrollbarSize="12dip">

<TableLayout android:id="@+id/answertable"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:stretchColumns="0,*" >
  </TableLayout>
</ScrollView>
<Relativelayout/>


****************************** JAVA code : ****************************

for (k =0; k< questionCount;k++ ) {
                    //tmpDict = trivialist.get(k);
                    Trivia trivia = trivialist.get(k);

                    TableRow row = new TableRow(getApplicationContext());

                    TextView tv1 = new TextView(getApplicationContext());
                    TextView tv2 = new TextView(getApplicationContext());
                    TextView tv3 = new TextView(getApplicationContext());
                    TextView tv4 = new TextView(getApplicationContext());

                     desc = trivia.getAnswerDesc();
                     quizquestion = trivia.getStrQuestion();

                    tv1.setText("\n\t" + quizquestion);
                    tv1.setTextColor(Color.WHITE); 
                    tv1.setSingleLine(false);
                    tv1.setLines(2);
                    tv1.setHorizontallyScrolling(false);
                    tv1.setTextSize(15);
                    tv1.setTypeface(null, Typeface.BOLD);

                            try {
                                //if the below value is null, it means that the selected answer is wrong 
                                // and enters to the else block
                                if (triviaDict.get("correctAnswer") != null) {
                                    String answer = (String) triviaDict.get("correctAnswer");
                                    tv2.setText("\n\t" + "You said: "+ answer + "\n");
                                    tv2.setTextColor(Color.WHITE);
                                    tv2.setSingleLine(false);
                                    tv2.setHorizontallyScrolling(false);
                                    tv2.setTextSize(15);
                                    tv2.setLines(2);
                                    tv2.setTypeface(null, Typeface.BOLD);

                                    tv3.setText("\t" +"That's Right."+ desc+"\n");
                                    tv3.setTextColor(Color.WHITE);
                                    tv3.setSingleLine(false);
                                    tv3.setHorizontallyScrolling(false);
                                    tv3.setTextSize(15);
                                    tv3.setLines(2);
                                    tv3.setTypeface(null, Typeface.BOLD);

                                    tv4.setText("\t" + "_____________________________________" );
                                    tv4.setTextColor(Color.WHITE);
                                    tv3.setHorizontallyScrolling(false);
                                    tv4.setTypeface(null, Typeface.BOLD);
                                } else {
                                    String wronganswer = (String) triviaDict.get("selected");
                                    tv2.setText("\n\t" + "You said:"+ wronganswer +"\n" );
                                    tv2.setTextColor(Color.WHITE);
                                    tv2.setTextSize(15);
                                    tv2.setLines(2);
                                    tv2.setSingleLine(false);
                                    tv2.setTypeface(null, Typeface.BOLD);

                                    tv3.setText("\t" + "Actually," + desc +"\n");
                                    tv3.setTextColor(Color.WHITE);
                                    tv3.setTextSize(15);
                                    tv3.setLines(2);
                                    tv3.setSingleLine(false);
                                    tv3.setTypeface(null, Typeface.BOLD);

                                    tv4.setText("\t" + "_____________________________________" );
                                    tv4.setTextColor(Color.WHITE);
                                    tv3.setHorizontallyScrolling(false);
                                    tv4.setTypeface(null, Typeface.BOLD);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }


                tv1.setLayoutParams(new TableRow.LayoutParams(0,
                                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));
                tv2.setLayoutParams(new TableRow.LayoutParams(0,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));
                tv3.setLayoutParams(new TableRow.LayoutParams(0,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));
                tv4.setLayoutParams(new TableRow.LayoutParams(0,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));


                    row.addView(tv1);
                    row.addView(tv2);
                    row.addView(tv3);
                    row.addView(tv4);
                    answerTable.addView(row);


                }
    <Relativelayout>

<ScrollView 
android:layout_marginTop="10dip" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="@+id/didyoudo"
android:layout_above="@+id/bardown" android:scrollbarFadeDuration="1000"
android:scrollbarSize="12dip">

<TableLayout android:id="@+id/answertable"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:stretchColumns="0,*" >
  </TableLayout>
</ScrollView>
<Relativelayout/>


****************************** JAVA code : ****************************

for (k =0; k< questionCount;k++ ) {
                    //tmpDict = trivialist.get(k);
                    Trivia trivia = trivialist.get(k);

                    TableRow row = new TableRow(getApplicationContext());

                    TextView tv1 = new TextView(getApplicationContext());
                    TextView tv2 = new TextView(getApplicationContext());
                    TextView tv3 = new TextView(getApplicationContext());
                    TextView tv4 = new TextView(getApplicationContext());

                     desc = trivia.getAnswerDesc();
                     quizquestion = trivia.getStrQuestion();

                    tv1.setText("\n\t" + quizquestion);
                    tv1.setTextColor(Color.WHITE); 
                    tv1.setSingleLine(false);
                    tv1.setLines(2);
                    tv1.setHorizontallyScrolling(false);
                    tv1.setTextSize(15);
                    tv1.setTypeface(null, Typeface.BOLD);

                            try {
                                //if the below value is null, it means that the selected answer is wrong 
                                // and enters to the else block
                                if (triviaDict.get("correctAnswer") != null) {
                                    String answer = (String) triviaDict.get("correctAnswer");
                                    tv2.setText("\n\t" + "You said: "+ answer + "\n");
                                    tv2.setTextColor(Color.WHITE);
                                    tv2.setSingleLine(false);
                                    tv2.setHorizontallyScrolling(false);
                                    tv2.setTextSize(15);
                                    tv2.setLines(2);
                                    tv2.setTypeface(null, Typeface.BOLD);

                                    tv3.setText("\t" +"That's Right."+ desc+"\n");
                                    tv3.setTextColor(Color.WHITE);
                                    tv3.setSingleLine(false);
                                    tv3.setHorizontallyScrolling(false);
                                    tv3.setTextSize(15);
                                    tv3.setLines(2);
                                    tv3.setTypeface(null, Typeface.BOLD);

                                    tv4.setText("\t" + "_____________________________________" );
                                    tv4.setTextColor(Color.WHITE);
                                    tv3.setHorizontallyScrolling(false);
                                    tv4.setTypeface(null, Typeface.BOLD);
                                } else {
                                    String wronganswer = (String) triviaDict.get("selected");
                                    tv2.setText("\n\t" + "You said:"+ wronganswer +"\n" );
                                    tv2.setTextColor(Color.WHITE);
                                    tv2.setTextSize(15);
                                    tv2.setLines(2);
                                    tv2.setSingleLine(false);
                                    tv2.setTypeface(null, Typeface.BOLD);

                                    tv3.setText("\t" + "Actually," + desc +"\n");
                                    tv3.setTextColor(Color.WHITE);
                                    tv3.setTextSize(15);
                                    tv3.setLines(2);
                                    tv3.setSingleLine(false);
                                    tv3.setTypeface(null, Typeface.BOLD);

                                    tv4.setText("\t" + "_____________________________________" );
                                    tv4.setTextColor(Color.WHITE);
                                    tv3.setHorizontallyScrolling(false);
                                    tv4.setTypeface(null, Typeface.BOLD);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }


                tv1.setLayoutParams(new TableRow.LayoutParams(0,
                                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));
                tv2.setLayoutParams(new TableRow.LayoutParams(0,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));
                tv3.setLayoutParams(new TableRow.LayoutParams(0,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));
                tv4.setLayoutParams(new TableRow.LayoutParams(0,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,10));


                    row.addView(tv1);
                    row.addView(tv2);
                    row.addView(tv3);
                    row.addView(tv4);
                    answerTable.addView(row);


                }
玩世 2025-01-15 07:58:00

最后,我对此提出了一些答案。首先,我保留了一个整数计数来确定单行中适合的字符数。之后计算字符串中的字符总数。然后将第一个整数计数除以字符串中的字符总数。然后你会得到一个整数。根据我的要求,我为该整数添加了+2。然后将textview的行数设置为最终获得的数字。

这样,问题就解决了。

Finally, I came up with some answer for this. First thing is I kept an integer count to fix how much characters to be fit in a single line. After that count the total no of characters in the string. Then divide the first integer count by total no of characters in the string. Then you will get an integer number. For my required, I added +2 for that integer number. Then set the lines of the textview to finally obtained number.

Thus, problem solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文