1

我想像 UITabBarController 在 iPhone 中所做的那样,我有四个类,我想在一个 tabber 中工作

当我单击第一个选项卡时,单击第二个选项卡时应显示第一类,单击第三个选项卡时应显示第二类,单击第四个选项卡时应显示第三类,应显示第四类

什么是在这种情况下操作的最好和最简单的方法,我是 Android 开发的新手,如果你不清楚问题,这是我的第一个应用程序,可以再问...

4

2 回答 2

0

这是在android中实现标签栏的Java代码

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

    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
    TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
    TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
    TabSpec fourthTabSpec = tabHost.newTabSpec("tid4");
    TabSpec fifthTabSpec = tabHost.newTabSpec("tid5");

    viewCache[0] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[1] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[2] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[3] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[4] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);

    firstTabSpec.setIndicator(viewCache[0]);
    secondTabSpec.setIndicator(viewCache[1]);
    thirdTabSpec.setIndicator(viewCache[2]);
    fourthTabSpec.setIndicator(viewCache[3]);
    fifthTabSpec.setIndicator(viewCache[4]);

    firstTabSpec.setContent(new Intent(this, HomeTabActivityGroup.class));
    secondTabSpec
            .setContent(new Intent(this,   ProfileTabActivityGroup.class));
    thirdTabSpec.setContent(new Intent(this,
            NotificationTabActivityGroup.class));
    fourthTabSpec.setContent(new Intent(this,
            FavoritesTabActivityGroup.class));
    fifthTabSpec
            .setContent(new Intent(this, MoreTabActivityGroupNew.class));

    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
    tabHost.addTab(thirdTabSpec);
    tabHost.addTab(fourthTabSpec);
    tabHost.addTab(fifthTabSpec);

// * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * * 这是 Tab 的 xml coed酒吧

 <?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">
<RelativeLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_alignParentBottom="true" 
        android:layout_height="wrap_content" 
        tabStripEnabled = "false"/>
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_alignParentTop="true"
        android:layout_above="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</RelativeLayout>

// * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * * 这是 Tab1 的 xml coed .xml 用于选项卡布局

   <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center">
  <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content"
        android:layout_height="50dip"></ImageView>
    </LinearLayout>
于 2011-07-01T12:58:24.190 回答
0

android 开发站点正好有您正在寻找的示例。http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

有代码示例。您需要制作一个选项卡布局 xml 文件,编辑 main.xml,然后在 java 代码中,对于每个选项卡,您将拥有类似这样的内容

intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
       res.getDrawable(R.drawable.ic_tab_albums))
      .setContent(intent);
tabHost.addTab(spec);
于 2011-07-01T11:41:23.953 回答