package com.example.haider_khan.simple_tablayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tab;
ViewPager vp;
MyPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MyPagerAdapter(getSupportFragmentManager());
toolbar = (Toolbar) findViewById(R.id.custom_appbar);
setSupportActionBar(toolbar);
tab = (TabLayout) findViewById(R.id.custom_TabLayout);
tab.addTab(tab.newTab().setText("Tab-1"));
tab.addTab(tab.newTab().setText("Tab-2"));
tab.addTab(tab.newTab().setIcon(R.drawable.saveaccount));
vp = (ViewPager) findViewById(R.id.custom_viewPager);
vp.setAdapter(adapter);
tab.setupWithViewPager(vp);
vp.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab));
}
问题:当我使用此选项卡时,无法在选项卡中加载图像或文本.setupWithViewPager(vp);如果我不使用它然后它工作正常,但然后选项卡没有连接到视图寻呼机。 我也试过getPagetitle一个方法从视图寻呼机它工作正常,但后来我不知道如何使用图标,因为它只适用于文本。
答案 0 :(得分:1)
将viewPager绑定到tabLayout之后可以使用以下代码:
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(R.drawable.play);
}