在Androd_Tab_view.java文件中,我有以下代码:
tabs =(TabHost)findViewById(R.id.TabHost1); tabs.setup();
TabHost.TabSpec first_tab = tabs.newTabSpec("tag1");
first_tab.setContent(new Intent(this,FirstTab.class));
first_tab.setIndicator("Book");
tabs.addTab(first_tab);
//SecondTab.class
TabHost.TabSpec second_tab = tabs.newTabSpec("tag2");
second_tab.setContent(new Intent(this,SecondTab.class));
second_tab.setIndicator("Authors");
tabs.addTab(second_tab);
在FirstTab.java文件中,我有以下代码:
public class FirstTab extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* First Tab Content */
TextView textView = new TextView(this);
textView.setText("First Tab");
setContentView(textView);
}
}
在SecondTab.java文件中,我有以下代码:
public class SecondTab extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); /* Second Tab Content */
TextView textView = new TextView(this);
textView.setText("Second Tab");
setContentView(textView);
}
}
但是当我尝试运行此代码时,我得到一个异常。什么可以解决这个问题??
答案 0 :(得分:0)
您可以像这样创建标签
void tabCreation() {
tabHost.setup();
TabSpec spec1 = tabHost.newTabSpec("Accounts");
spec1.setIndicator(createTabView(tabHost.getContext(), "Accounts", R.drawable.tab_home));
Intent inte = new Intent(TabsActivity.this, Activityone.class);
inte.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec1.setContent(inte);
// spec1.setContent(R.id.tab1);
TabSpec spec2 = tabHost.newTabSpec("Contacts");
spec2.setIndicator(createTabView(tabHost.getContext(), "Contacts", R.drawable.tab_account));
Intent _int = new Intent(TabsActivity.this, Activitytwo.class);
_int.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec2.setContent(_int);
// /spec2.setContent(R.id.tab2);
TabSpec spec3 = tabHost.newTabSpec("Chats");
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.setCurrentTab(0);
}
private static View createTabView(final Context context, final String text, final int id) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_indicator, null);
TextView tv = (TextView) view.findViewById(R.id.title);
tv.setText(text);
ImageView icon = (ImageView) view.findViewById(R.id.icon);
icon.setImageResource(id);
return view;
}
}
您的活动
public class FirstTab extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accounts);
/* First Tab Content */
TextView textView = new TextView(this);
textView.setText("First Tab");
setContentView(textView);
和
public class secondTab extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_accounts);
/* First Tab Content */
TextView textView = new TextView(this);
textView.setText("First Tab");
setContentView(textView);
}
}
和tis http://www.androidhive.info/2011/08/android-tab-layout-tutorial/