【问题标题】:How do you put FragmentTabhost on bottom你如何将 FragmentTabhost 放在底部
【发布时间】:2013-03-22 18:26:44
【问题描述】:

我想把 tabwidget 像 iPhone App 一样放在底部。 我知道这种布局违反了 Google UserInterface 指南,但我需要。

所以,我在 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 的错误.....

试试这个

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

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    试试这个:

    <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>
    

    【讨论】:

      【解决方案2】:

      试试这个:

      <?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>
      

      【讨论】:

      • 仍然在顶部。
      【解决方案3】:

      试试这个,遵循 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 放在了底部。检查。
      • 嗯...我将 xml 替换为您的,但 tabhost 是顶部....请上传您的项目?
      • 嗯,这既不可能也不建议上传整个项目,您可以检查您在代码中所做的任何其他事情,以防止 tabhost 对齐底部。卸载应用程序,然后重新安装,因为这在我的应用程序中运行良好。
      • 好的,我卸载了这个应用程序并重新安装,但选项卡是顶部。之后,我将模拟器 Android 版本从 4.2 更改为 2.3.3 但不起作用....
      • 然后确保您将我的 xml 粘贴到正确的文件中,即R.layout.activity_top
      猜你喜欢
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 2021-02-07
      • 2019-07-30
      相关资源
      最近更新 更多