无法在选项卡中获得不同的视图
我有一个带有 2 个选项卡的应用程序,第一个是列表视图,第二个我正在尝试制作 TextView。问题是我不知道如何让 TextView 显示出来。列表视图正在工作,但我根本无法在 TextView 上获取任何内容。我尝试使用“Hello,World”将文本添加到选项卡中,但我无法弄清楚。
我的带有 TabWidget 的 main.xml 部分如下所示:
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/itemlist" />
<TextView
android:id="@+id/HelloAndroid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
ListView 正在带有 mTabHost 代码的 java 文件上使用,
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Answer").setContent(R.id.itemlist));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Ask").setContent(R.id.HelloAndroid));
mTabHost.setCurrentTab(0);
}
TextView 位于名为 HelloAndroid.java 的不同文件上,代码如下所示:
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
如何修复它以便第二个选项卡显示示例文本? 谢谢!
I have an app with 2 tabs, the first is a listview, and the second I'm trying to make a TextView. The problem is I have no idea on how to get the TextView to show up. The listview is working, but I can't get anything on the TextView at all. I tried using the Hello, World to try to work with the text into the tab, but I can't figure it out.
My main.xml section with the TabWidget looks like this:
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/itemlist" />
<TextView
android:id="@+id/HelloAndroid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
ListView is being used on the java file with the mTabHost code,
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Answer").setContent(R.id.itemlist));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Ask").setContent(R.id.HelloAndroid));
mTabHost.setCurrentTab(0);
}
The TextView is on a different file called HelloAndroid.java, and the code looks like:
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
How can I fix it so that the 2nd tab brings up the example text?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您希望将 HelloAndroid 活动显示为第二个选项卡的内容。如果我是对的,
你应该这样称呼:
但您必须记住,您的 HelloAndroid 活动与布局文件中的 TextView 没有任何共同之处。
因此,最好不要将活动设置为选项卡内容,而是从布局文件中更改文本视图的值。
附言。请记住将您的 HelloAndroid 活动放入 Manifest.xml
If I understand correctly you want to display the HelloAndroid activity as a content of second tab. If I'm right, instead of this:
you should call this:
But you have to bear in mind that your HelloAndroid activity has nothing in common with TextView in your layout file.
So probably it would be better not to set the activity as a tab content but to change the value of text view from your layout file.
PS. Remember to put your HelloAndroid activity to Manifest.xml