图标不会与Android Tablayout中的文本相邻。

时间:2017-05-10 06:57:11

标签: android android-tablayout

我正在尝试使用文本和图标(彼此相邻)构建TabLayout。 但是图标正在文本上方。 我这样做是为了添加图标:

   private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(R.drawable.email_id_icon);
    tabLayout.getTabAt(1).setIcon(R.drawable.email_id_icon);
}

这是我的完整代码。请告诉我,我做错了什么?

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vets_listing);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();
}

private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(R.drawable.email_id_icon);
    tabLayout.getTabAt(1).setIcon(R.drawable.email_id_icon);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new WalkInFragment(),"Walk in");
    adapter.addFragment(new HomeVisitFragment(),"Home Visit");

    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以根据自己的意愿创建自定义标签布局并定位图标。 以下是有关如何执行此操作的示例代码。

创建custom_tab.xml布局文件。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:textSize="15sp"
/>

将您的设置图标方法更改为此

private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("ONE");
tabOne.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.arrow_down_float, 0, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);

TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("TWO");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.checkbox_on_background, 0, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);

TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText("THREE");
tabThree.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.arrow_up_float, 0, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);

}

这样做。这是一个指向自定义选项卡布局实现的github链接。 Tab with icons