0

嗨,我正在学习谷歌名称 hello-tabwidget 提供的教程。创建选项卡菜单。一切正常,但现在我想向一个选项卡添加一个按钮,但此按钮出现在所有选项卡中。

请问有人可以帮忙吗?

谢谢

这就是我所拥有的

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

Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost();  // The activity TabHost
TabHost.TabSpec spec;  // Resusable TabSpec for each tab
Intent intent;  // Reusable Intent for each tab

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, FirstTab.class);

// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("First Tab").setIndicator("First Tab",
                  res.getDrawable(R.drawable.ic_tab_artists))
              .setContent(intent);
tabHost.addTab(spec);

// Do the same for the other tabs
intent = new Intent().setClass(this, SecondTab.class);
spec = tabHost.newTabSpec("Second Tab").setIndicator("Second Tab",
                  res.getDrawable(R.drawable.ic_tab_albums))
              .setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, ThirdTab.class);
spec = tabHost.newTabSpec("Third Tab").setIndicator("Third Tab",
                  res.getDrawable(R.drawable.ic_tab_songs))
              .setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, NextTab.class);
spec = tabHost.newTabSpec("Next Tab").setIndicator("Next Tab",
                  res.getDrawable(R.drawable.ic_tab_next))
              .setContent(intent);
tabHost.addTab(spec);

tabHost.setCurrentTab(0);
}
}

主要的.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">

    <FrameLayout android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="5dp">

       <include layout="@layout/tab1"/>
       <include layout="@layout/tab2"/>
       <include layout="@layout/tab3"/>
       <include layout="@layout/tab4"/>

    </FrameLayout>

  <TabWidget android:id="@android:id/tabs" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent"   
   android:gravity="bottom"/>

 </LinearLayout>
</TabHost>

我为每个选项卡创建了 xml 布局,这是一个带有按钮的选项卡,其他选项卡完全相同,只是没有按钮标签和不同的 id

tab2.xml

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

<Button android:text="@+id/Button01" 
  android:id="@+id/Button01"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content">
</Button>
 </LinearLayout>

我为每个选项卡创建了类这是来自第二个选项卡的代码,我想要一个按钮其他类完全相同只是

setContentView(R.layout.tab2);

设置为指向不同的布局

SecondTab.java

public class SecondTab extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tab2);


 }
}

有任何想法吗 ??

谢谢

4

1 回答 1

1

解决了!!!

在 main.xml 我包括了这 4 行:

<include layout="@layout/tab1"/>
   <include layout="@layout/tab2"/>
   <include layout="@layout/tab3"/>
   <include layout="@layout/tab4"/>

那些线不应该在那里

所以 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">

   <FrameLayout android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:padding="5dp">


     </FrameLayout>

    <TabWidget android:id="@android:id/tabs" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"   
    android:gravity="bottom"/>

    </LinearLayout>
    </TabHost>
于 2011-01-06T21:18:20.143 回答