1

我需要一个TabLayout在初始阶段只显示两个选项卡的选项卡。
滑动活动标签后,将在中心,下一个标签将在左侧,上一个标签将在右侧。
我怎样才能做到这一点?

以下是确切的场景快照: 请任何人给我建议。?在此处输入图像描述 在此处输入图像描述

4

5 回答 5

1

将此插入到您要自定义 tabLayout 的代码中:

 tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
 tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
于 2017-11-09T09:58:41.340 回答
0

在 .xml 中添加此代码

   <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        android:id="@+id/toolbar"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"/>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
  </android.support.design.widget.CoordinatorLayout>

如果你想改变 tabmode 你可以做 app:tabMode="fix"

java代码是

public class Tabmenu extends AppCompatActivity {
 private TabLayout tabLayout;
  private ViewPager viewPager;
   Toolbar toolbar;

 //  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
     protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_tabmenu);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Menus Item");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

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

    private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new 
    ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new Norththalis(), "NORTH INDIAN THALIS");
    adapter.addFrag(new Northmenu(), "NORTH INDIAN MENU");
    adapter.addFrag(new Biryani(), "BIRYANI");
    adapter.addFrag(new Packs(), "PACKS");
    adapter.addFrag(new Roles(), "ROLES");
    adapter.addFrag(new Sides(), "SIDES");
    adapter.addFrag(new Sweets(), "SWEETS");
    adapter.addFrag(new Beverages(), "BEVERAGES");

    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 addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

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

           }
于 2017-11-09T09:24:24.633 回答
0

利用

<android.support.v4.view.PagerTabStrip
   android.support.v4.view.PagerTabStrip
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />

xmlViewPager中

于 2018-02-05T08:04:05.103 回答
0

经过长时间的搜索,通过使用 PagerSlidingTabStrip 库得到了解决方案。

应用程序:pstsPaddingMiddle="true" 应用程序:pstsTabPaddingLeftRight="80dp"

这成功了。

于 2017-11-13T06:02:49.453 回答
0

像魅力一样工作

你的标签 xml

  <com.google.android.material.tabs.TabLayout
    android:id="@+id/tab_layout"
    android:layout_gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

在设置 viewpager 适配器后添加这个

  for (int i = 0; i < tabLayout.getTabCount(); i++) {
        View tab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(i);
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) tab.getLayoutParams();
        if (i == 0) {
            p.setMargins(400, 0, 200, 0);
        } else {
            p.setMargins(0, 0, 200, 0);
        }

        tab.requestLayout();
    }
于 2020-07-10T10:10:07.770 回答