Is it possible to define each tab's content in a separate XML file?
I would like to define each tab's content in a different XML file so the visual editor in Eclipse can be used. That is, without all of the tabs being laid over each other. The visual editor doesn't even seem to work with the XML example provided here: [link text][1] [1]: http://developer.android.com/guide/tutorials/views/hello-tabwidget.html "Here", It just has a Null pointer exception.
I have tried to define each tap in it's own file, for example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</RelativeLayout>
And when adding the tab I use:
mTabHost.addTab(mTabHost.newTabSpec("tab_in").setIndicator("Input").setContent(R.layout.input));
But this causes the application to crash on launch. Is there any way to do this?
开发者_高级运维Thanks
Just my 2cents. I had lot of problems using an Intent based tabhost. I ran into really strange behaviours expecially with ListView widget. I was able to rid all of my problems when I started to rewrite my project into a View based tabhost instead of using separated Intent/activities
You can always have your separated XML per Tab
In short this is what I did:
public class TabOneView implements TabContentFactory {
private LayoutInflater mInflater;
public TabOneView(Context context){
mInflater = LayoutInflater.from(context);
}
@Override
public View createTabContent(String arg0) {
View v = mInflater.inflate(R.layout.tab_one, null);
/**
* Add your code here for buttons/list etc
* An object lookup for your Button:
*/
Button b = (Button)v.findViewById(R.id.btn01);
b.setOnClickListener(mListener);
//...more code
return v;
}
}
It's much more convenient if you write a TabContentFactory, or have a separate Activity for each tab.
you might also look at using the TabHost, which allows you to have each tab be an 'activity'
I think what you're doing would work if you had valid XML.
The TextView is giving you those errors because of the android:text="@+id/TextView01"
part. Change that to android:text="Some literal string"
and it should work (or android:text="@strings/tabtextview01"
assuming a strings.xml file with that string).
Your XML should be:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@strings/tabTextView01" /><!--
or android:text="The string literal to display." -->
</RelativeLayout>
精彩评论