我在更新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);
}
答案 0 :(得分:0)
您需要为TextView提供一个ID,以便稍后找到它:
textView.setId(1234);
然后:
TextView textView = findViewById(1234);
textView.setText("New Text");
答案 1 :(得分:0)
如果要保留的相对布局中没有任何子视图,请在relativeLayout.removeAllViews()
循环之前调用for
。否则,请使用removeView(View)
或removeViewAt(int)
删除您不想要的内容。