在 android 中使用选项卡布局 - 添加到一个选项卡的按钮会出现在所有选项卡中

发布于 2024-10-11 01:45:18 字数 3665 浏览 4 评论 0原文

您好,我正在关注 google 名称 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);
}
}

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

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


 }
}

有什么想法吗?

谢谢

Hi I'm following tutorial provided by google name hello-tabwidget.
To create tab menu.
Everything works fine but now I want to add a button to one tab but
this button appears in all tabs.

Please can anyone help?

Thanks

this is what i have

@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);
}
}

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

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

I created xml layout for each tab this is one with the button
others are exactly this same just with out a button tag and with different 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>

and i created class for each tab this is code from second tab where i want to have a button
the other classes are exactly this same just

setContentView(R.layout.tab2);

is set to point to different layouts

SecondTab.java

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

    setContentView(R.layout.tab2);


 }
}

Any ideas ??

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扎心 2024-10-18 01:45:18

解决了!!!

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

solved it!!!

in main.xml i included those 4 lines:

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

those lines shouldn't be there

so main.xml looks like that now:

  <?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文