我想用android中的操作栏实现带标题条的滑动标签。 我已经实现了它的工作正常,除了标题条不滚动,选项卡可以滚动。我想要标签标题条滚动就像播放商店的标题条。 我怎样才能实现它......
我试过以下
main_activity
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#403333"
android:textColor="#000000"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
main.java
TabsPageAdapter tabsPageAdapter;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabsPageAdapter = new TabsPageAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(tabsPageAdapter);
}
tabsPageAdapter.java
final int TAB_COUNT = 6;
public TabsPageAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
case 3:
return new Fragment4();
case 4:
return new Fragment5();
case 5:
return new Fragment6();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return TAB_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Frag 1";
case 1:
return "Frag 2";
case 2:
return "Frag 3";
case 3:
return "Frag 4";
case 4:
return "Frag 5";
case 5:
return "Frag 6";
}
return "Not Set";
}
答案 0 :(得分:0)
这就是我使用视图寻呼机的方式:
xml代码:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager_contacts">
<android.support.v4.view.PagerTitleStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title_contacts_groups"
android:layout_gravity="top"
android:paddingTop="4dp"
android:background="#33EE34"
android:paddingBottom="4dp">
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
调用代码:
viewPager = (ViewPager) findViewById(R.id.pager_contacts);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new FragmentContactAdaptor(fragmentManager));