3

我已经尝试了几件事来在我的活动中的“滚动选项卡”的选项卡中显示文本,但它从未显示

任何想法发生了什么?

下面是代码:

可滚动标签.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??如果是的话....为什么??)

thx 的帮助家伙:)

4

1 回答 1

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 为我工作。

于 2015-10-23T11:26:01.327 回答