2011-11-14 41 views
0

我试图创建一个具有2-tabbed活动的应用程序,其中tab1上有一张图片和一些文本,tab2有两个按钮用户可以点击(这应该导致进一步的活动)Android Tabs:第二个选项卡中的内容隐藏在第一个选项卡的内容下

由于某种原因,我无法获得tab2上的按钮来显示;我只能看到与tab1相同的图片。仔细观察,我发现这些按钮隐藏在tab1的图片下。我究竟做错了什么?我如何让这张照片消失,而不是显示在tab2上?

以下是我的代码:

IndexActivity

public class IndexActivity extends TabActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    TabHost.TabSpec spec = getTabHost().newTabSpec("tab1"); 
    spec.setContent(R.id.tab1content); 
    spec.setIndicator("tab1"); 
    getTabHost().addTab(spec); 

    spec = getTabHost().newTabSpec("tab2"); 
    spec.setContent(R.id.tab2content); 
    spec.setIndicator("tab2"); 
    getTabHost().addTab(spec); 

    getTabHost().setCurrentTab(0);  

    } 
} 

我的main.xml文件是这样的:

<?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="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="5dp"> 
      <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">  
       <include layout="@layout/tab1"/> 
       <include layout="@layout/tab2"/>    
      </FrameLayout>   
</LinearLayout> 

tab1.xml是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <ImageView android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:scaleType="center" 
        android:src="@drawable/pic"/> 
     <ScrollView android:id="@+id/scrolltab1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
     <TextView android:id="@+id/tab1content" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
        android:background="#65000000" 
        android:text="@string/text" 
      android:textColor="#ffffffff" 
      android:textSize="20sp" 
       android:padding="15dp" 
     /> 
     </ScrollView>  
</merge> 

TAB2是这样的:

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

    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.10" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.10" 
     android:text="Button" /> 

</LinearLayout> 

请帮帮忙!我一定是被忽视的东西,但我想不出我什么sugggest :(

回答

0

main.xml取下FrameLayout以下两行,并看看会发生什么。

<include layout="@layout/tab1"/> 
<include layout="@layout/tab2"/>    
+0

我有点需要这两个布局,虽然... – nubicurio

+0

但你试过看看发生了什么?原因是你多次添加视图。当您调用'setContentView(R.layout.main)'时,将自动添加'tab1.xml'和'tab2.xml'的布局,但是您将创建'TabSpecs',其中第一个包含'TextView'(tab1content )来自'tab1.xml',第二个包含'tab2.xml'(tab2content)的整个布局。当您调用'spec.setContent(...)'时,由于main.xml中的元素,您正手动重复布局文件内容的通货膨胀,因此setContentView(R.layout.main)已完成。 – Squonk

+0

我拿出包含文件,应用程序根本无法启动(强制关闭);奇怪的是,我似乎通过将android:src更改为android:background来解决问题。但是你做了什么很有意义......这是否意味着我实际上有4个选项卡,即使我只看到两个选项卡?我不知道我在做什么:( – nubicurio

0

你看看这个....

你例子其实公平地代表这个例子.... android tabhost tutorial

我希望你已经看过了....这是我用来当我弗里斯特开发了我的第一个Android标签插件的一个...

PS:你看看这个例子中,上述的part 2 .. 。延续每一件事,你需要知道的一切。

+0

谢谢!我会在早上看看这第一件事... – nubicurio

+0

它解决了你的问题...如果是的话,你会介意我的答案作为你的问题的答案..如果没有...请问你能告诉我你试过的东西, – medampudi

0

看你的代码和XML我认为这个问题是与该行tab1.xml:

<TextView android:id="@+id/tab1content"... 

尝试将布局ImageViewScrollView左右。并将android:id="@+id/tab1content"移动到布局。事情是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <LinearLayout android:id="@+id/tab1content" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <ImageView android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:scaleType="center" 
       android:src="@drawable/pic"/> 
     <ScrollView android:id="@+id/scrolltab1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
     <TextView android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="#65000000" 
      android:text="@string/text" 
      android:textColor="#ffffffff" 
      android:textSize="20sp" 
      android:padding="15dp" /> 
     </ScrollView> 

    </LinearLayout> 
</merge> 
+0

嘿,按钮现在显示在tab2上;但是,tab1中的文本“@ string/text”不再可见:( – nubicurio

+0

是的,这与布局有所不同,您应该在此主题中标记答案并针对新问题提出一个新问题。 – C0deAttack

+0

其他的东西,但我改变了android:src到android:背景,修复了我的问题... – nubicurio

相关问题