操作栏未显示

3

我使用TabHostTabSpec在Android中创建了一个具有三个选项卡的项目。我通过TabActivity扩展了我的主活动。问题是应用程序上没有显示ActionBar。我使用了Holo.Light.DarkActionBar主题。

MainActivity.java

     public class MainActivity extends TabActivity {

    TabHost tabhost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    tabhost=getTabHost();

    TabHost.TabSpec homespec=tabhost.newTabSpec("Home");
    homespec.setIndicator("Home",null);
    Intent homeintent=new Intent(this,HomeActivity.class);
    homespec.setContent(homeintent);

    TabHost.TabSpec eventspec=tabhost.newTabSpec("Event");
    eventspec.setIndicator("Event");
    Intent eventintent=new Intent(this,EventActivity.class);
    eventspec.setContent(eventintent);

    TabHost.TabSpec profilespec=tabhost.newTabSpec("Profile");
    profilespec.setIndicator("Profile");
    Intent profileintent=new Intent(this,ProfileActivity.class);
    profilespec.setContent(profileintent);

    tabhost.addTab(homespec);
    tabhost.addTab(eventspec);
    tabhost.addTab(profilespec);
    }
    }

activity_main.xml

活动_主.xml
<TabHost
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@android:id/tabhost"
    android:layout_weight="1">

    <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="fill_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                />

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"/>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"/>
        </FrameLayout>
    </LinearLayout>
</TabHost>

有没有其他的方法可以实现这个?也给我提供一些建议吧。

2
我们可以看一下你的activity_main.xml文件吗? - KodyVanRy
@DuhProgrammer13 当然可以。我已经更新了我的问题,附带activity_main.xml文件。 - Ranjith
2个回答

4

在AndroidManifest.xml中为此活动设置主题:

<activity android:name=".MainActivity"
            android:theme="@android:style/Theme.Holo.Light"/>

打开activity_main,点击Design->Theme(在顶部栏上,在“设备名称”和“横屏”旁边)->Manifest Themes->选择您的主题。

编辑: 当然,如果您没有有意地忘记,请添加此内容。

<?xml version="1.0" encoding="utf-8"?>

2

将您的布局更改为以下内容:

<?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="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <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="fill_parent" >
        </FrameLayout>
    </LinearLayout>

</TabHost>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接