[英]Tabs not showing up using tabhost
這是我的代碼,用於在屏幕頂部顯示三個選項卡,但在屏幕上未顯示任何選項卡,而且我絲毫不知道為什么會發生這種情況,有人可以指導我這里是XML
<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"
android:layout_marginTop="78dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
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" />
</LinearLayout>
</TabHost>
這是java代碼
final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setCurrentTab(1);
final TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
final TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
final TabHost.TabSpec tab3 = tabHost.newTabSpec("Third Tab");
tab1.setIndicator("Upcoming").setContent(new Intent(this, Upcoming_matches.class));
tab2.setIndicator("Recent").setContent(new Intent(this, Recent_matches.class));
tab3.setIndicator("Fixture").setContent(new Intent(this, Fixtures.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
tabHost.getTabWidget().getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams((int) Math.ceil((width / 1)), 60));
tabHost.getTabWidget().getChildAt(1).setLayoutParams(new LinearLayout.LayoutParams((int) Math.ceil((width / 3)), 60));
tabHost.getTabWidget().getChildAt(2).setLayoutParams(new LinearLayout.LayoutParams((int) Math.ceil((width / 3)), 60));
找到了答案,在@Don的幫助下,它基本上是錯誤的布局,框架布局低於tabwidget,這是正確的XML
<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"
android:layout_marginTop="78dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<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="wrap_content" />
</LinearLayout>
</TabHost>
請在tabconent中將fill_parent更改為wrap_content,這里是xml
<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"
android:layout_marginTop="78dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.