android - studio - Cómo agregar el ícono para pestañas deslizables
tablayout android (4)
Agregue .setIcon (resources.getDrawable (DrawableIDHere)) en su bucle for, y también modifique su bucle for un poco.
// 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
Además, ¡no olvides poner las ID dibujables correctas en tu matriz de ICONOS!
Estoy descargando el código desde here . Quiero mostrar las pestañas con el ícono. ¿Cómo puedo ayudarme?
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) {
}
}
Hola echa un vistazo a la biblioteca ViewPagerIndicator .
Este es un muy buen ejemplo para las pestañas que se pueden deslizar.
Si está utilizando un TabLayout
, simplemente haga esto (este ejemplo utiliza tres pestañas):
//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]);
// 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