Динамический TextView не умещается в мультилинии - PullRequest
0 голосов
/ 24 февраля 2012

У меня проблема с моим приложением для Android.Я хочу динамически создавать 4 TableRows в TableLayout.Каждая из этих строк будет содержать 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);

                }

Но ничего не работает идеально.Это работает для 3-го текстового обзора, но не для 1-го.Не знаю, что происходит.

Может кто-нибудь сказать мне, что делать.

Обновление

У меня нет проблем с этим кодомза исключением того, что только тв3 переходят к следующей строке большого текста, но не тв1.Я хочу, чтобы в случае большого текста

все tv1, tv2 и tv3 продолжались в следующей строке

Ответы [ 2 ]

0 голосов
/ 05 марта 2012

Наконец-то я придумал ответ на этот вопрос.Во-первых, я вел счетчик целых чисел, чтобы определить количество символов в одной строке.После этого подсчитайте общее количество символов в строке.Затем разделите первое число целых чисел на общее количество символов в строке.Тогда вы получите целое число.Для моего необходимого я добавил +2 для этого целого числа.Затем установите строки текстового представления в окончательно полученный номер.

Итак, проблема решена.

0 голосов
/ 25 февраля 2012
    <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);


                }
...