我已经尝试了几项在我的活动中“滚动标签”的标签中显示文字的内容,但它从未显示
任何想法发生了什么?
下面是代码:
scrollabletabs.xml:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/pager">
<android.support.v4.view.PagerTitleStrip
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_gravity="top"
android:background="@color/material_deep_teal_500"
android:id="@+id/tabtitle"
style="@style/pagertitlestrip">
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
ScrolllableTabs.java:
package com.example.pc.learn_again;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class ScrollableTabs extends FragmentActivity {
//get the ViewPager//
ViewPager viewPager=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scrollabletabs);
viewPager = (ViewPager) findViewById(R.id.pager);
//the connection between the ViewPager and the adapter is below//
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new myadapter(fragmentManager));
}
}
class myadapter extends FragmentPagerAdapter
{
public myadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//as you see from the below this is how we tell the Viewpager which fragment (page) to be shown//
Fragment fragment=null;
if (position==0)
{
fragment = new ScrollableFragment1();
}
if (position==1)
{
fragment = new ScrollableFragment2();
}
if (position==2)
{
fragment = new ScrollableFragment3();
}
return fragment;
}
@Override
public int getCount() {
return 3;//this is the number of pages
}
//the below is for the titles//
@Override
public CharSequence getPageTitle(int position) {
if (position==0)
{
return "Login Screen";
}
if (position==1)
{
return " List View";
}
if (position==2)
{
return "Calculator";
}
return null;
}
}
让我指出片段正确显示并且它们工作正常,但我不知道为什么标签中的标题不显示(该方法是否可能返回null ??如果是的话......为什么??)
对于帮助人员来说:)答案 0 :(得分:1)
使用:
<android.support.v4.view.PagerTabStrip
android:id="@+id/strip"
android:layout_gravity="top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:textColor="#fff"
android:background="@android:color/holo_blue_bright"></android.support.v4.view.PagerTabStrip>
而不是
<android.support.v4.view.PagerTitleStrip
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_gravity="top"
android:background="@color/material_deep_teal_500"
android:id="@+id/tabtitle"
style="@style/pagertitlestrip">
PageTabStrip为我工作。