如何在TabWidget的每个标签中添加更多项目?

时间:2013-08-20 20:36:50

标签: android android-tabhost android-tabs

我已经使用TabHost和TabWidget编写了一个简单的标签程序,但是我只能为每个标签添加一个项目..我想添加一个textview,一个imageview和一个按钮但是我无法...这是我的xml代码:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

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

        <TextView
            android:id="@+id/tab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Tab1"
            android:textColor="#ffffff"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Tab2"
            android:textColor="#ffffff"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tab3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Tab3"
            android:textColor="#ffffff"
            android:textSize="16sp"
            android:textStyle="bold" />

    </FrameLayout>

</LinearLayout>
</TabHost>

如何将imageview与现有的textview一起添加到Tab1?

1 个答案:

答案 0 :(得分:0)

您可以在操作栏中使用标签,如下所示:

private ViewPager mPager;
private PagerAdapter mPagerAdapter;
private static final int NUM_PAGES = 3;
onCreate()

中的

final ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabReselected(Tab tab,
                android.app.FragmentTransaction ft) {

        }

        @Override
        public void onTabSelected(Tab tab,
                android.app.FragmentTransaction ft) {
            mPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(Tab tab,
                android.app.FragmentTransaction ft) {
        }

    };

    for (int i = 0; i < mPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar
                .newTab()
                .setText(mPagerAdapter.getPageTitle(i))
                .setIcon(
                        ((ScreenSlidePagerAdapter) mPagerAdapter)
                                .getPageIcon(i))
                .setTabListener(tabListener));
    }

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return Fragment.instantiate(MainActivity.this,
                    Fragment_sports.class.getName());
        case 1:
            return Fragment.instantiate(MainActivity.this,
                    Fragment_casino.class.getName());
        case 2:
            return Fragment.instantiate(MainActivity.this,
                    Fragment_live_betting.class.getName());
        default:
            break;
        }
        return null;
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String tabLabel = null;
        switch (position) {
        case 0:
            tabLabel = " Sports";
            break;
        case 1:
            tabLabel = "Casino";
            break;
        case 2:
            tabLabel = "Live Betting";
            break;
        }

        return tabLabel;
    }

    public int getPageIcon(int position) {
        int id = 0;
        switch (position) {
        case 0:
            id = R.drawable.icon_all_sports_d;
            break;
        case 1:
            id = R.drawable.icon_favourites_d;
            break;
        case 2:
            id = R.drawable.icon_live_d;
            break;
        default:
            break;
        }
        return id;

    }
}

和您的 main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>