我想要有一个布局文件和两个文本视图,并根据动态条件,我想选择显示多少个文本字段。 附带的代码显示了两个领域的所有:-( 背后这样设计的动机是为了避免维护两个活动,并期待以后的分歧和感觉的时间。 任何帮助表示赞赏。Android如何仅显示布局的一些字段
代码调用文本视图。创作,其中m是基于其他UI事件决定为0
或1
的目标是显示m
数目的文本的观点:
for (int i = 0; i < m; i++) {
b = new B_ContactCollector(this, i);
c.add(i, b);
}
创建文本视图的代码
switch (n) {
case 0:
tv = (TextView) f.findViewById(R.id.tv1);
b = (Button) f.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get a new Contact
Intent i = new Intent (f, B_ContactLister.class);
i.putExtra("windowNumber", 0);
f.startActivityForResult(i,1);
}
});
break;
case 1:
tv = (TextView) f.findViewById(R.id.tv2);
b = (Button) f.findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get a new Contact
Intent i = new Intent (f, B_ContactLister.class);
i.putExtra("windowNumber", 1);
f.startActivityForResult(i,1);
}
});
break;
default:
// throw an error that it shouldn't have come here
break;
}
的布局是:
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:layout_alignLeft="@+id/tv2"
android:layout_alignParentTop="true"
android:background="#FF9D85"
android:maxLines="5"
android:scrollbars="vertical" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv1"
android:text="+" />
<TextView
android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/tv1"
android:layout_marginTop="20dp"
android:background="#FF9D85"
android:maxLines="5"
android:scrollbars="vertical" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv2"
android:layout_alignLeft="@+id/tv2"
android:text="+" />
<ImageView
android:id="@+id/intro"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/intro" />
谢谢。在我的情况下,因为我想显示m个窗口而不是第m个窗口,所以您建议的代码稍作修改。我将粘贴它我自己的问题添加代码,因为评论不让我。 – 2014-12-09 14:39:57