Android 如何为可滑动选项卡添加图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21528687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add the icon for swipeable tabs
提问by user3114723
I am downloading the code from herei want to dispaly tabs with icon how can posssible please help me.
我正在从这里下载代码我想显示带有图标的选项卡怎么可能请帮助我。


public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Text", "Photo", "Record" ,"Tag"};
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
final int[] ICONS = new int[] {
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
};
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
回答by Ojonugwa Jude Ochalifu
If you are using a TabLayout, just do this (This example uses three tabs):
如果您使用的是TabLayout,只需执行此操作(此示例使用三个选项卡):
//An array containing your icons from the drawable directory
final int[] ICONS = new int[]{
R.drawable.icon_1,
R.drawable.icon_2,
R.drawable.icon_3
};
//Get reference to your Tablayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setIcon(ICONS[0]);
tabLayout.getTabAt(1).setIcon(ICONS[1]);
tabLayout.getTabAt(2).setIcon(ICONS[2]);
回答by Jake JediMastr Ellis
Add .setIcon(resources.getDrawable(DrawableIDHere)) in your for loop, and also alter your for loop a bit.
在你的 for 循环中添加 .setIcon(resources.getDrawable(DrawableIDHere)) ,并稍微改变你的 for 循环。
// Adding Tabs
for (int i=0; i < tabs.length; i++)
{
actionBar.addTab(actionBar.newTab().setText(tabs[i])
.setIcon(resources.getDrawable(ICONS[i]))
.setTabListener(this));
}//endfor
Also, don't forget to put the right drawable ID's in your ICONS array!
另外,不要忘记将正确的可绘制 ID 放入您的 ICONS 数组中!
回答by user2480902
// here you add an array of your icon
final int[] ICONS = new int[] {
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
};
// add this following code to solve your problem
// here NewsFeedActivity.this.getResources() is used to get the drawable folder resource
// instead of NewsFeedActivity you have to use your activity name here
for (int i=0; i < tabs.length; i++)
{
actionBar.addTab(actionBar.newTab().setText(tabs[i])
.setIcon(NewsFeedActivity.this.getResources().getDrawable(ICONS[i]))
.setTabListener(this));
}//endfor
回答by dinesh sharma
Hi check out the ViewPagerIndicatorlibrary.
您好,请查看ViewPagerIndicator库。
This is very good example for swipe-able tabs.
这是可滑动选项卡的很好示例。

