3つのタブがあるアプリケーションがあります。すべてのタブが1つのレイアウトを開きます。includeオプションを使用します。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:id="@+id/tab1_ref"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/tab1" />
<include
android:id="@+id/tab2_ref"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/tab2" />
<include
android:id="@+id/tab3_ref"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/tab3" />
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
そして、メインアクティビティには次のコードがあります。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Prueba con TABS
Resources res = getResources();
TabHost tabs=(TabHost)findViewById(android.R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("Tab 1");
spec.setContent(R.id.tab1_ref);
spec.setIndicator("Tab 1", null);
tabs.addTab(spec);
//Dont WORK the Intent
// Intent tab1Intent = new Intent(this, Tab1Activity.class);
// spec.setContent(tab1Intent);
spec=tabs.newTabSpec("Tab 2");
spec.setContent(R.id.tab2_ref);
spec.setIndicator("Tab 2", null);
tabs.addTab(spec);
spec=tabs.newTabSpec("Tab 3");
spec.setContent(R.id.tab3_ref);
spec.setIndicator("Tab 3", null);
tabs.addTab(spec);
tabs.setCurrentTab(0);
tabs.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
Toast.makeText(getApplicationContext(),"Pulsada pestaña: " + tabId, Toast.LENGTH_LONG).show();
}
});
//Fin prueba tabs
}
この作品では、さまざまなタブを押すと、さまざまなレイアウトが表示されます。
しかし、新しいレイアウトをアクティブ化したときに、どのようにして新しいアクティビティを開くことができますか?たとえば、tab2をプッシュすると、tab2activity.classが開きます。これを試しましたが、例外が表示されます:
Intent tab1Intent = new Intent(this, Tab1Activity.class);
spec.setContent(tab1Intent);
新しいレイアウトをアクティブ化するときに、どのように新しいアクティビティを開くことができますか?
よろしくお願いします。