在AsyncTask的末尾添加TextView

时间:2012-03-22 17:18:33

标签: android user-interface textview android-activity

我需要在从AsyncTask远程检索后向ScrollableLayout添加文本。由于我不知道所涉及的字符串数量,因此我需要以编程方式根据需要创建尽可能多的TextView

布局代码

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_phone_mainView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/view_phone_linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top" >

        <TextView
            android:id="@+id/view_phone_lblTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal|center_vertical"
            android:text="@string/view_phone_title"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/view_phone_lblWarning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/view_phone_warning"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:visibility="invisible" />

    </LinearLayout>

</ScrollView>

活动

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        pDialog = new ProgressDialog(this);
        pDialog.setIndeterminate(true);
        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setMessage(getString(R.string.view_phone_wait));

        pDialog.show();

        task = new MessageLoaderTask(); //Returns String[] on callback
        task.execute((Void) null);
    }

    public void onRulesLoaded(String[] messages) { //Directly called by OnPostExecute
        LinearLayout container = (LinearLayout) findViewById(R.id.view_phone_linearLayout);
        if (messages != null) {

            for (String m : messages) {
                TextView tv = new TextView(this);
                tv.setText(m);
                tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
                tv.setVisibility(View.VISIBLE);
                tv.setBackgroundColor(Color.TRANSPARENT);
                tv.setTextColor(Color.BLACK);

                container.addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            }
        }

        pDialog.dismiss();
    }

我确定,通过调试,正确评估了字符串。结果是标题标签显示,警告隐藏但下面只有白色......

我尝试滚动并注意到滚动区域的长度是veeeeeeeeeery长,与我用于测试的长Lorem ipsum存根文本兼容。如果我通过调试将其中一个字符串截断为空(只有2个),则可滚动区域的高度会更短。我使用灯光主题,所以我期待白色背景上的黑色文字。

tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
tv.setVisibility(View.VISIBLE);
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setTextColor(Color.BLACK);
当一切都失败时,第二次添加

。它们是否到位没有区别。我该怎么办?

由于

2 个答案:

答案 0 :(得分:1)

如果这是唯一的问题,不确定为什么ScrollView的长度会增加,但似乎需要将LinearLayout的方向设置为垂直。

答案 1 :(得分:0)

使用以下代码。

   <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>

通过在字符串之间保留'\ n',将所有字符串添加到此EditText。这样每件事都能正确对齐。