我正在尝试使用片段布局创建标签。没有设置屏幕的全宽,也没有显示标签文本。没有找到任何解决方案来解决这个问题。下面我添加了输出的代码和截图以及我想要实现的内容,请检查任何帮助将不胜感激。
主要活动
public class SettingActivity extends Fragment {
TabLayout tab;
ViewPager vp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_setting, container, false);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.vp);
viewPager.setAdapter(new ViewPagering(getFragmentManager(), tabLayout.getTabCount()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
}
ViewPager
public class ViewPagering extends FragmentStatePagerAdapter {
int noTabs;
private String[] tabTitles = new String[]{"Tab1", "Tab2"};
public ViewPagering(FragmentManager fm, int noTabs) {
super(fm);
this.noTabs = noTabs;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Frag1 f1 = new Frag1();
return f1;
case 1:
frag2 f2 = new frag2();
return f2;
default:
return null;
}
}
@Override
public int getCount() {
return noTabs;
}
}
XML
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="rarecrew.com.taberp.SettingActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="30dp"
app:tabMaxWidth="0dp"
app:tabMode="fixed"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>