gpt4 book ai didi

android - TabLayout 的标签不显示

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:48:55 28 4
gpt4 key购买 nike

我有一个主 Activity ,它承载一个 fragment ,而 fragment 又承载一个 TabLayout(带有 ViewPager)。显示标签栏,但不显示标签本身。

这是我在主要 Activity 中用于显示主机 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.):

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:

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:

<?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.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-TabLayout 的 Google Android 指南

我在这里缺少什么?

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

最佳答案

这为我修复了它:

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

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

关于android - TabLayout 的标签不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31544617/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com