带有片段的Android TABS

问题描述:

我一直在研究一个由带有多个选项卡的GUI组成的小型项目。我对碎片的概念颇为陌生,但它似乎是android选项卡环境中的最佳实践。我设法编写了一个简单的标签应用程序,在3个标签之间切换。带有片段的Android TABS

我唯一的问题是,当我尝试将标签放在屏幕的底部。当它们位于底部时,选项卡部件将覆盖容器(本例中为framelayout),因此不会发生任何事情,单击各个选项卡时屏幕保持不变。

下面的XML布局表示了良好的版本(其中标签是在屏幕的顶部):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <TabHost 
     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" 
      > 

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

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

      <FrameLayout 
       android:id="@+android:id/realtabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1"/> 
     </LinearLayout> 
    </TabHost> 
</LinearLayout> 

下面的XML布局表示未使用的版本,当标签处于屏幕的底部:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
     <RelativeLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 

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

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

      <FrameLayout 
       android:id="@+android:id/realtabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
      /> 
     </RelativeLayout> 
    </TabHost> 
</LinearLayout> 

我改变了layour到RelativeLayout的和添加了此命令:机器人:layout_alignParentBottom =“真”的tabwidget。有没有人有我如何解决这个问题的另一种想法?提前致谢。

一般情况下,在屏幕底部使用制表符是一个不好的解决方案(请参阅Android design guidelines)。

对于您的解决方案:将TabWidget放在列表视图的末尾。由于绘图是一个线程和碎片,所以你的情况已经变得黯淡了上方TabWidget

+0

亚历克斯,我不能说出你最后一句话。如果有拼写错误或重新编写它,请您尝试重新输入吗? – 2012-09-17 03:48:55

你可以使用weight来代替。尝试此布局标记

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android: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="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

</android.support.v4.app.FragmentTabHost> 

您可以用自己的TabHost类替换FragmentTabHost。所以,你的标签将在屏幕的底部。 要小心嵌套的权重(它们对性能不利),并且在视图层次结构中很深入(特别是对于Android < 4)。

+0

一般来说,你并不需要“假”标签内容FrameLayout(在你的情况下它有id =“@ android:id/tabcontent”,如果不需要的话删除它们中的一个,并用主要的tabcontent fragmelayout id替换为“@android :ID/tabcontent“)。想想看。 – Alexander 2014-10-21 01:25:45