0

我面临一个更新TextView内容的问题RelativeLayout。当我第二次更新时,我需要从RelativeLayout.

RelativeLayout我的应用中有一个activity_calendar.xml

<RelativeLayout
            android:id="@+id/rel_container"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/linearLayout1"
            android:layout_toRightOf="@+id/linearLayout1">

</RelativeLayout>

这是主要代码:

public void listall () {
  RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rel_container);
    int prevTextViewId = 0;
    for (int i = 0; i < titleCalendar.length; i++) {
        final TextView textView = new TextView(this);
        textView.setText(titleCalendar[i]);
        cnvrttime = hourCalendar[i] - 3;
        addcnvrtTime = 60*cnvrttime;
        curTextViewId = prevTextViewId + 1;
        textView.setId(curTextViewId);
        final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

        params.addRule(RelativeLayout.BELOW, addcnvrtTime);
        params.setMargins(0, convertDPtoInt((float)addcnvrtTime), 0, 0);
        textView.setLayoutParams(params);

        prevTextViewId = curTextViewId;
        relativeLayout.addView(textView, params);

}
4

2 回答 2

0

如果您想要保留的相对布局中没有任何子视图,请在循环relativeLayout.removeAllViews()之前调用。for否则,使用removeView(View)removeViewAt(int)删除您不想要的。

于 2013-07-25T15:07:14.840 回答
0

您需要为 TextView 提供一个 id,以便稍后找到它:

textView.setId(1234);

然后后来:

TextView textView = findViewById(1234);
textView.setText("New Text");
于 2013-07-25T12:25:42.227 回答