android : tabhost 不呈现选项卡
android : tabhost not rendering tabs
我有一个简单的activity
public class MainActivity extends Activity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost)findViewById(R.id.tabHost);
tabHost.setup();
tabHost.newTabSpec("a");
tabHost.newTabSpec("b");
tabHost.newTabSpec("c");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是 xml 的视图定义
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="activities.MainActivity"
android:visibility="visible">
<TabHost
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tabHost"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<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"
android:visibility="visible"
android:background="#ff0007ff"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="visible"
android:background="#ff122dff"></LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
我知道 tabhost 的视图有空白内容,但我希望在这个 activity 启动时至少呈现选项卡的视图
但是我看到此 activity 的空白屏幕,我错过了什么?
您必须将每个选项卡项添加到选项卡小部件,如下所示:
tabHost = (TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setContent(R.id.tab1);
tab1.setIndicator("Tab 1");
tabHost.addTab(tab1);
选项卡是使用片段实现的。在 xml 中,您需要使用 android.support.v4.app.FragmentTabHost class 而不是 TabHost。在 java 中,您需要扩展 FragmentActivity,还需要手动在 FragmentTabHost 中添加选项卡。给出了完整的例子 here.
我有一个简单的activity
public class MainActivity extends Activity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost)findViewById(R.id.tabHost);
tabHost.setup();
tabHost.newTabSpec("a");
tabHost.newTabSpec("b");
tabHost.newTabSpec("c");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是 xml 的视图定义
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="activities.MainActivity"
android:visibility="visible">
<TabHost
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tabHost"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<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"
android:visibility="visible"
android:background="#ff0007ff"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="visible"
android:background="#ff122dff"></LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
我知道 tabhost 的视图有空白内容,但我希望在这个 activity 启动时至少呈现选项卡的视图
但是我看到此 activity 的空白屏幕,我错过了什么?
您必须将每个选项卡项添加到选项卡小部件,如下所示:
tabHost = (TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setContent(R.id.tab1);
tab1.setIndicator("Tab 1");
tabHost.addTab(tab1);
选项卡是使用片段实现的。在 xml 中,您需要使用 android.support.v4.app.FragmentTabHost class 而不是 TabHost。在 java 中,您需要扩展 FragmentActivity,还需要手动在 FragmentTabHost 中添加选项卡。给出了完整的例子 here.