我正在尝试将com.viewpagerindicator.TabPageIndicator
放入SherlockFragment
。虽然ViewPager
中内容的滚动效果很好,但TabPageIndicator的样式无法正常工作。此寻呼机包含五个片段,其标题不适合屏幕宽度。我在Activity中工作得很好但是当我在Fragment中尝试使用它时,所有五个标题都被切断以适应屏幕的宽度。
我尝试使用LayoutInflater
和Fragment#onCreateView
中提供的getSystemService
两者都会产生相同的结果。
不确定是否相关,但我在此布局中也有com.actionbarsherlock.widget.SearchView,并且未应用所有属性。
my_search_layout.xml
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/indicator"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_below="@id/indicator"
/>
</RelativeLayout>
MySearchFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LayoutInflater themedInflater = (LayoutInflater) getSherlockActivity()
.getSupportActionBar()
.getThemedContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View root = themedInflater.inflate(R.layout.my_search_layout, container, false);
FragmentManager fm = ((Fragment)this).getChildFragmentManager();
mSectionsPagerAdapter = new SectionsPagerAdapter(fm);
mViewPager = (ViewPager) root.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabPageIndicator indicator = (TabPageIndicator) root.findViewById(R.id.indicator);
indicator.setViewPager(mViewPager);
return root;
}
SectionsPagerAdapter
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
Log.d(TAG, "SectionsPagerAdapter: " + i);
fragment = new HelloFragment();
return fragment;
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.section_title_page_1);
case 1:
return getString(R.string.section_title_page_2);
case 2:
return getString(R.string.section_title_page_3);
case 3:
return getString(R.string.section_title_page_4);
case 4:
return getString(R.string.section_title_page_5);
}
return null;
}
}