我正在使用ViewPager和ViewPagerIndicator库,我使用TabPagerIndicator,问题是虽然我可以从一个页面导航到另一个页面并且正确地看到与每个页面对应的标题,但是我看不到哪个标签是活动的,哪个不是。 在库代码可用的屏幕截图中,我可以看到在所选标签的标题下绘制的蓝线,但在我的应用程序中,我根本看不到该行。 我尝试通过代码更改一些资源但没有得到任何东西。 任何帮助将不胜感激。 thx提前
编辑: 我的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>
活动代码
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;
}
}
答案 0 :(得分: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
属性