私はAndroidアプリ開発の初心者です。入れ子になったタブを作成しようとしました。まず、3つのタブを作成して、最初のタブのコンテンツを別のタブのアクティビティーとして定義します。私がやったが、以下に説明される:Androidの初心者、なぜ入れ子のタブが機能しないのですか?
私はメインタブのアクティビティを定義した(最初のタブの内容を別のタブの活動であることを):
のres /レイアウト/ main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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:id="@+id/textview2"
android:layout_width="fill_parent"
android: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>
を
私のメインタブのアクティビティクラス:
public class MyTest extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = getTabHost();
//the first tab's content is another tabs activity
Intent tabs2=new Intent(this, SecondTabsActivity.class);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(tabs2));
//other tabs' content are just TextView
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);
}
}
あなたが上に見たように、私は別のタブの活動であることを最初のタブの内容を好きなので、I Fでしょう最初に第2レベルのタブの意図を定義し、最初のタブの内容をその意図に設定します。
第2レベルのタブレイアウト:
RES /レイアウト/ level2tabs.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a tab" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android: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>
レベルに対して対応するクラス2のタブ:
public class SecondTabsActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level2tabs);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.layout.nestedtabs));
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);
}
しかし、私は実行アプリケーションが予期せず停止しました。私は入れ子のタブでどこが間違っているのか分かりませんか? }
あなたのlogcatエラーを表示します。どのようなエラーが発生しているのですか? – Sujit
@Sujit、私は初心者です、私は論理エラーを表示する方法を知らない? Eclipseコンソールから、エラーメッセージが表示されませんでした。エミュレータからエラーメッセージが表示されます。「アプリケーションが予期せず停止しました。再試行してください。」 – Leem
このhttp://developer.android.com/を参照してください。ガイド/開発/デバッグ/ ddms.html – Sujit