android viewpagerindicator tabpageIndicator 选项卡未显示 on/off

android viewpagerindicator tabpageIndicator tabs are not showing on/off

我正在使用 ViewPager 和 ViewPagerIndicator 库,我使用的是 TabPagerIndicator,问题是虽然我可以从一个页面导航到另一个页面并正确查看每个页面对应的标题,但我看不到哪个选项卡处于活动状态以及哪个选项卡处于活动状态不是。 在库代码提供的屏幕截图中,我可以看到在所选选项卡的标题下绘制了一条蓝线,但在我的应用程序中我根本看不到那条线。 我尝试通过代码更改一些资源,但一无所获。 任何帮助将不胜感激。 提前谢谢

编辑: 我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.example.apple.gallerapplication.GalleryActivity"
   >
    <com.viewpagerindicator.TabPageIndicator
        android:id="@+id/indicator"
        android:padding="10dip"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_alignParentTop="true"
        android:textColor="@android:color/black"
        android:foregroundGravity="center"
        />

        />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/indicator"/>
</RelativeLayout>

activity代码

public class GalleryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);

        //viewpager
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new CustomPagerAdapter(this));


        TabPageIndicator mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(viewPager);


    }
}

寻呼机适配器:

public class CustomPagerAdapter extends PagerAdapter implements IconPagerAdapter {

private Context mContext;

public CustomPagerAdapter(Context context) {
    mContext = context;
}

@Override
public Object instantiateItem(ViewGroup collection, int position) {
    CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
    LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewGroup layout = (ViewGroup) inflater.inflate(customPagerEnum.getLayoutResId(), collection, false);
    collection.addView(layout);
    return layout;
}

@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
    collection.removeView((View) view);
}

@Override
public int getCount() {
    return CustomPagerEnum.values().length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public CharSequence getPageTitle(int position) {
    CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
    return mContext.getString(customPagerEnum.getTitleResId());

}



@Override
public int getIconResId(int index) {
    return 0;
}
}

在 Xml 试试这个:

 <!-- use tablayout to display tabs  -->
<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    style="@style/AppTabLayout"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/your_color" />

在 res/values/styles.xml

<style name="AppTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">@color/colorAccent</item>    //indicator color
    <item name="tabIndicatorHeight">4dp</item>                //indicator height
    <item name="tabPaddingStart">6dp</item>
    <item name="tabPaddingEnd">6dp</item>
    <item name="tabSelectedTextColor">@color/color_white</item>
</style>

在这里您可以自定义 tabIndicator 属性