如何将带有文本的图标添加到滑动标签

时间:2014-08-16 14:54:59

标签: java android mobile android-tabs

我正在尝试向我的标签添加带有文字的图标,但它无效,

我尝试添加

  

getResources()getDrawable(R.drawable.ic_launcher));

它仍然无法正常工作。当我运行应用程序时,标签栏只有文本,没有图标。

这是我的代码。

package com.tabs_swipe;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;


public class MainActivity extends FragmentActivity implements
        OnTabChangeListener, OnPageChangeListener {

    private TabsPagerAdapter mAdapter;
    private ViewPager mViewPager;
    private TabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        // Tab Initialization
        initialiseTabHost();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        // Fragments and ViewPager Initialization

        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(MainActivity.this);
    }

    // Method to add a TabHost
    private static void AddTab(MainActivity activity, TabHost tabHost,
            TabHost.TabSpec tabSpec) {
        tabSpec.setContent(new MyTabFactory(activity));
        tabHost.addTab(tabSpec);
    }

    // Manages the Tab changes, synchronizing it with Pages
    public void onTabChanged(String tag) {
        int pos = this.mTabHost.getCurrentTab();
        this.mViewPager.setCurrentItem(pos);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    // Manages the Page changes, synchronizing it with Tabs
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        int pos = this.mViewPager.getCurrentItem();
        this.mTabHost.setCurrentTab(pos);
    }

    @Override
    public void onPageSelected(int arg0) {
    }

    // Tabs Creation
    private void initialiseTabHost() {

        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        // TODO Put here your Tabs
        MainActivity.AddTab(
                this,
                this.mTabHost,
                this.mTabHost.newTabSpec("ButtonTab").setIndicator("ButtonTab",
                        getResources().getDrawable(R.drawable.ic_launcher)));
        MainActivity.AddTab(
                this,
                this.mTabHost,
                this.mTabHost.newTabSpec("ImageTab").setIndicator("ImageTab",
                        getResources().getDrawable(R.drawable.ic_launcher)));
        MainActivity.AddTab(
                this,
                this.mTabHost,
                this.mTabHost.newTabSpec("TextTab").setIndicator("TextTab",
                        getResources().getDrawable(R.drawable.ic_launcher)));

        mTabHost.setOnTabChangedListener(this);

        //

        //

    }

}

TabsPagerAdapter

package com.tabs_swipe;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new ButtonFragment();
        case 1:
            // Games fragment activity
            return new ImageFragment();
        case 2:
            // Movies fragment activity
            return new TextFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }

}

MyTabFactory

package com.tabs_swipe;

import android.content.Context;
import android.view.View;
import android.widget.TabHost.TabContentFactory;

public class MyTabFactory implements TabContentFactory {

    private final Context mContext;

    public MyTabFactory(Context context) {
        mContext = context;
    }

    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }
}

1 个答案:

答案 0 :(得分:0)

修改你的tabspageradapter,示例在这里:

public class TabPagerAdapter扩展了FragmentStatePagerAdapter实现         IconPagerAdapter {

protected static final String[] headers = new String[] { "HOME",
        "BOOKMARK", "SEARCH", "INFO", "SETTING" };

private static final int[] ICONS = new int[] { R.drawable.selector_home,
        R.drawable.selector_bookmark, R.drawable.selector_search,
        R.drawable.selector_info, R.drawable.selector_settings };

public TabPagerAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}

private int mCount = CONTENT.length;

@Override
public Fragment getItem(int i) {
    switch (i) {
    case 0:
        return new HomeFragment();

    case 1:
        return new FavoritesFragment();
    case 2:
        return new SearchFragment();

    case 3:
        return new AboutUsFragment();

    case 4:
        return new SettingsFragment();
    }
    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mCount; // No of Tabs
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}

@Override
public CharSequence getPageTitle(int position) {
    Log.e("pos", "" + position);
    return TabPagerAdapter.CONTENT[position % headers.length];
}

public void setCount(int count) {
    if (count > 0 && count <= 10) {
        mCount = count;
        notifyDataSetChanged();
    }
}

@Override
public int getIconResId(int index) {
    return ICONS[index % ICONS.length];
}

}

这里是代码为view pager indicator的例子......