Hello, TabWidget each tab refer to new xml
I'm using Google's exmaple of Hello, TabWidget but altered it to look like this:
main.xml:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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">
<TextView
android:text="@+layout/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
an开发者_开发百科droid:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
java file:
public class HelloTabWidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.layout.text));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));
mTabHost.setCurrentTab(0);
}
}
and here is the text.xml in res/layout:
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="This is Tab 1" />
</LinearLayout>
What I'm basically trying to do is have each tab refer to its own xml file rather than all in main.xml, but the text in the first tab doesn't show up.
Are you trying to have each tab refer to its own Activity
? If so, you can set up an intent as the content for each tab:
intent = new Intent().setClass(this, Test.class);
spec = tabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(intent);
tabHost.addTab(spec);
Of course, you'd then have to have a class named Test (or whatever else) that sets test.xml
as the layout.
public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
}
This tutorial will help you further if this is the type of scenario that you're trying to set up.
There's three options for tabs using the built-in tab host.
- Multiple views on a single activity with one layout file
- Multiple activities
- Custom views built in a TabContentFactory
It sounds like you want #3. What you need to do is something like this:
setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag)
{
View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);
// Setup the view here
return view;
}});
View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);
That's not quite right as LayoutInflater.inflate takes two parameters.
精彩评论