• <bdo id='WgAiI'></bdo><ul id='WgAiI'></ul>

    <small id='WgAiI'></small><noframes id='WgAiI'>

      <legend id='WgAiI'><style id='WgAiI'><dir id='WgAiI'><q id='WgAiI'></q></dir></style></legend>
    1. <tfoot id='WgAiI'></tfoot>

    2. <i id='WgAiI'><tr id='WgAiI'><dt id='WgAiI'><q id='WgAiI'><span id='WgAiI'><b id='WgAiI'><form id='WgAiI'><ins id='WgAiI'></ins><ul id='WgAiI'></ul><sub id='WgAiI'></sub></form><legend id='WgAiI'></legend><bdo id='WgAiI'><pre id='WgAiI'><center id='WgAiI'></center></pre></bdo></b><th id='WgAiI'></th></span></q></dt></tr></i><div id='WgAiI'><tfoot id='WgAiI'></tfoot><dl id='WgAiI'><fieldset id='WgAiI'></fieldset></dl></div>

        TabLayout 的选项卡未显示

        时间:2023-10-04
          <legend id='a014t'><style id='a014t'><dir id='a014t'><q id='a014t'></q></dir></style></legend>
          • <bdo id='a014t'></bdo><ul id='a014t'></ul>

                  <tbody id='a014t'></tbody>
                <i id='a014t'><tr id='a014t'><dt id='a014t'><q id='a014t'><span id='a014t'><b id='a014t'><form id='a014t'><ins id='a014t'></ins><ul id='a014t'></ul><sub id='a014t'></sub></form><legend id='a014t'></legend><bdo id='a014t'><pre id='a014t'><center id='a014t'></center></pre></bdo></b><th id='a014t'></th></span></q></dt></tr></i><div id='a014t'><tfoot id='a014t'></tfoot><dl id='a014t'><fieldset id='a014t'></fieldset></dl></div>

                • <tfoot id='a014t'></tfoot>

                  <small id='a014t'></small><noframes id='a014t'>

                  本文介绍了TabLayout 的选项卡未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个主要活动,它承载一个片段,而片段又承载一个 TabLayout(带有 ViewPager).显示标签栏,但不显示标签本身.

                  I have a main activity, which hosts a fragment, which in turn hosts a TabLayout (with a ViewPager). The tab bar is shown, baut the tabs themselves are not shown.

                  这是我在主要活动中用于显示主机片段的代码:

                  Here is my code in the main activity for displaying the host fragment:

                          Fragment fragment = new BMITabsFragment();
                  
                          FragmentManager fragmentManager = getSupportFragmentManager();
                          fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(Constants.BMI_TABS_FRAGMENT).commit();
                  

                  这是我托管 TabLayout 的 Fragment,即 BMITabsFragment (s.a.):

                  Here is my the Fragment which hosts the TabLayout, which is BMITabsFragment (s.a.):

                  public class BMITabsFragment extends Fragment {
                  ...
                  @Override
                  public void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                      if (getArguments() != null) {
                          mParam1 = getArguments().getString(ARG_PARAM1);
                          mParam2 = getArguments().getString(ARG_PARAM2);
                      }
                  
                  }
                  
                  @Override
                  public void onViewCreated(View view, Bundle savedInstanceState) {
                      super.onViewCreated(view, savedInstanceState);
                  
                      // Get the ViewPager and set it's PagerAdapter so that it can display items
                      ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
                      viewPager.setAdapter(new BMIFragmentPagerAdapter(getActivity().getSupportFragmentManager(),
                              getActivity()));
                  
                      // Give the TabLayout the ViewPager
                      TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs);
                      tabLayout.setupWithViewPager(viewPager);
                  }
                  
                  @Override
                  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                           Bundle savedInstanceState) {
                      // Inflate the layout for this fragment
                      View view = inflater.inflate(R.layout.fragment_bmitabs, container, false);
                      return view;
                  }
                  ...
                  }
                  

                  这是我的 FragmentPagerAdapter:

                  This is my FragmentPagerAdapter:

                  public class BMIFragmentPagerAdapter extends FragmentPagerAdapter {
                  
                  final int PAGE_COUNT = 2;
                  private FragmentManager fragmentManager;
                  private Context context;
                  
                  public BMIFragmentPagerAdapter(FragmentManager fm, Context context) {
                      super(fm);
                      this.context = context;
                      this.fragmentManager = fm;
                  
                  }
                  
                  public BMIFragmentPagerAdapter(FragmentManager fm) {
                      super(fm);
                      fragmentManager = fm;
                  
                  }
                  
                  @Override
                  public CharSequence getPageTitle(int position) {
                      String[] pageTitles = context.getResources().getStringArray(R.array.page_titles_array);
                      return pageTitles[position];
                  }
                  
                  @Override
                  public Fragment getItem(int position) {
                      SharedPreferences prefs = context.getSharedPreferences(Constants.SHARED_PREFS_FILE, 0);
                      long patientId = prefs.getLong(Constants.SELECTED_PATIENT_ID, 1);
                      Fragment fragment = null;
                      switch (position){
                          case 0:
                              return BMITabelleFragment.newInstance(patientId);
                  
                          case 1:
                              return BMIChartFragment.newInstance(patientId);
                  
                          default:
                              return BMITabelleFragment.newInstance(patientId);
                      }
                  }
                  
                  @Override
                  public int getCount() {
                      return PAGE_COUNT;
                  }
                  }
                  

                  这是fragment_bmitabs.xml:

                  And this is the fragment_bmitabs.xml:

                  <?xml version="1.0" encoding="utf-8"?>
                  

                  <android.support.design.widget.TabLayout
                      android:id="@+id/sliding_tabs"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      app:tabMode="scrollable" />
                  
                  <android.support.v4.view.ViewPager
                      android:id="@+id/viewpager"
                      android:layout_width="match_parent"
                      android:layout_height="0px"
                      android:layout_weight="1"
                      android:background="@android:color/white" />
                  
                  </LinearLayout>
                  

                  我的代码基于 https://github 上的 Google Android 指南.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-TabLayout

                  我在这里缺少什么?

                  注意:我正在使用 AppCompatActivity 和支持库 v4 &v7 和 com:android:support:design 库

                  Note: I am using AppCompatActivity and the support libraries v4 & v7 and the com:android:support:design library

                  推荐答案

                  这为我解决了问题:

                  tabLayout.post(new Runnable() {
                      @Override
                      public void run() {
                          tabLayout.setupWithViewPager(viewPager);
                      }
                  });
                  

                  https://code.google.com/p/android/issues/detail?id=180462

                  这篇关于TabLayout 的选项卡未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何通过 FragmentPagerAdapter 将变量传递给 Fragment? 下一篇:通过按钮单击更改 viewpager 片段

                  相关文章

                  1. <i id='H0ibI'><tr id='H0ibI'><dt id='H0ibI'><q id='H0ibI'><span id='H0ibI'><b id='H0ibI'><form id='H0ibI'><ins id='H0ibI'></ins><ul id='H0ibI'></ul><sub id='H0ibI'></sub></form><legend id='H0ibI'></legend><bdo id='H0ibI'><pre id='H0ibI'><center id='H0ibI'></center></pre></bdo></b><th id='H0ibI'></th></span></q></dt></tr></i><div id='H0ibI'><tfoot id='H0ibI'></tfoot><dl id='H0ibI'><fieldset id='H0ibI'></fieldset></dl></div>
                      <bdo id='H0ibI'></bdo><ul id='H0ibI'></ul>

                      <tfoot id='H0ibI'></tfoot>
                    1. <small id='H0ibI'></small><noframes id='H0ibI'>

                    2. <legend id='H0ibI'><style id='H0ibI'><dir id='H0ibI'><q id='H0ibI'></q></dir></style></legend>