简体   繁体   English

如何使用图标和文本TabLayout android创建选项卡

[英]How to create Tabs with icons and text TabLayout android

I am working on an android app and wants to create a layout like below. 我正在开发一个android应用,想要创建一个如下所示的布局。

在此处输入图片说明

I want to make the Tabs same as shown in this layout which will be scrolling horizontally. 我想使标签与此布局中显示的相同,它将水平滚动。 I want to set the icons of reactions and text on these tabs. 我想在这些选项卡上设置反应和文本图标。 The Icons are the server images which I need to show, but I have no idea how can I show server url images in the tabs as icons. 图标是我需要显示的服务器图像,但是我不知道如何在选项卡中将服务器URL图像显示为图标。

Is there any other way of making this layout except TabLayout or Sliding TabLayout with which I can get the same UI as in image? 除了可以使我获得与图像中相同的UI TabLayoutTabLayout之外,还有其他任何制作此布局的方法吗?

Please help if anyone know how to do this. 如果有人知道该怎么做,请提供帮助。

ViewPager Adapter ViewPager适配器

public class ViewPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 2;
private Context mContext;
private String[] tabText;



public ViewPagerAdapter(FragmentManager fragmentManager, Context context) {
    super(fragmentManager);
    this.mContext = context;
    this.tabText = new String[]{
            "First tab",
            "second tab"
    };



}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new FirstFragment();
        case 1:
            return new SecondFragment();
        default:
            return null;
    }
}

@Override
public int getCount() {
    return NUM_ITEMS;
}

  @Override
  public CharSequence getPageTitle(int position) {
   return tabText[position];
 }

} }

MainActivity.Java MainActivity.Java

    private int[] imageResId = {// icon resource };
    mTabLayout = (TabLayout) view.findViewById(R.id.tl_viewer);
    mViewpager = (ViewPager) view.findViewById(R.id.vp_viewer);
    mTabLayout.setupWithViewPager(mViewpager);


   //Method to set up tab layout icon
   private void setUpTabIcon() {
    for (int i = 0; i < mTabLayout.getTabCount(); i++) {

        mTabLayout.getTabAt(i).setIcon(imageResId[i]);


    }

}

Tab count and Tab text picked from ViewPager Adapter. 从ViewPager适配器选择的选项卡数和选项卡文本。 and icon picked from setTabIcon method 和从setTabIcon方法中选取的图标

Please use Below link of Android hive. 请使用Android配置单元的以下链接。 Android Hive Explain very well for working with TabLayout - Android Hive非常适合与TabLayout配合使用-

http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/ http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

You will need to have an custom layout for your tabs. 您需要为选项卡使用自定义布局。 See this link How to set icon next to text in tablayout 查看此链接如何在TabLayout中的文本旁边设置图标

And to answer your another query of displaying the server images, you can use a library for that purpose like http://square.github.io/picasso/ 为了回答显示服务器映像的另一个查询,您可以为此目的使用一个库,例如http://square.github.io/picasso/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM