如何将FragmentTabhost放在底部

4
我希望将 TabWidget 放在底部,就像 iPhone 应用程序一样。 我知道这种布局违反了 Google 用户界面指南,但是我需要它。
所以,我在 Stackoverflow 上阅读了许多类似的帖子。 但是每个帖子都不能正常工作。
我想使用 FragmentTabhost,而不使用外部库(例如ActionBarsherlock)。
这是我的代码。
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_above="@android:id/tabs" />

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_above="@android:id/tabs" />

    <TabWidget
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" />
</RelativeLayout>

以下是活动代码

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_top);

    //set up FragmentTabhost
    FragmentTabHost host = (FragmentTabHost)findViewById(R.id.tabhost);
    host.setup(this,getSupportFragmentManager(),R.id.content);

    //add tabspec
    TabSpec tabSpec1 = host.newTabSpec("tab1");
    Button btn1 = new Button(this);
    btn1.setBackgroundResource(R.drawable.icon1);
    tabSpec1.setIndicator(btn1);
    Bundle bundle1 = new Bundle();
    bundle1.putString("name", "tab1");
    host.addTab(tabSpec1,SampleFragment.class,bundle1);


    TabSpec tabSpec2 = host.newTabSpec("tab2");
    Button btn2 = new Button(this);
    btn2.setBackgroundResource(R.drawable.icon2);
    tabSpec2.setIndicator(btn2);
    Bundle bundle2 = new Bundle();
    bundle2.putString("name", "tab2");
    host.addTab(tabSpec2,SampleFragment1.class,bundle2);

}

所以我尝试了另一种布局,像这样。
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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" >

    <FrameLayout
        android:id="@+id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0" />

    <TabWidget
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal" />

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

请帮助我,


注意:最终我解决了这个问题。 这是SupportPackage的一个bug......

尝试这个:

http://code.google.com/p/android/issues/detail?id=40035

3个回答

2

尝试一下这个:

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TabWidget
    android:id="@android:id/tab"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

<FrameLayout
    android:id="@android:id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</RelativeLayout>

0

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost 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">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:layout_weight="0"/>
    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

仍然处于前列。 - A J

-2

试试这个,以下的 XML 对我非常有效。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost  xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@android:id/tabhost"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >

<RelativeLayout
    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"
        android:layout_alignParentBottom="true" />

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v4.app.FragmentTabHost>

希望这个能够运行。

我现在已经粘贴了完整的 XML,它对我来说很好用,而且它将 TabHost 放在底部。请检查。 - Usama Sarwar
我把xml替换成了你的,但是tabhost在顶部...请上传你的项目? - dmnlk
上传整个项目既不可能也不推荐,您可以检查代码中是否有阻止选项卡设置在底部的任何其他操作。卸载该应用程序,然后重新安装,因为这在我的应用程序中完美运行。 - Usama Sarwar
好的,我卸载了这个应用并重新安装,但选项卡仍然在顶部。之后,我将模拟器的Android版本从4.2更改为2.3.3,但还是不起作用... - dmnlk
然后请确保您将我的XML粘贴到正确的文件中,即“R.layout.activity_top”。 - Usama Sarwar
我创建了一个名为testlayout.xml的新XML文件,粘贴了您的代码,并删除了旧的XML文件(因为ID冲突)。之后,在Activity中出现了错误(R.id.tabhost为空),所以我更改了android:id="@+id/tabhost"并更改了其他ID。运行应用程序后,选项卡仍然在顶部。 - dmnlk

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