这是我的PagerTitleStrip的XML:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="500dp"
android:scaleType="centerCrop">
<android.support.v4.view.PagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/viepagertitlestrip"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
以下是我的自定义PagerAdapter类:
public class CustomPagerAdapter extends PagerAdapter {
private int[] image_resources = {
R.drawable.1,
R.drawable.2,
R.drawable.3,
};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomPagerAdapter(Context ctx) {
this.ctx = ctx;
}
@Override
public int getCount() {
return image_resources.length;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return (view == (RelativeLayout) o);
}
@Override
public CharSequence getPageTitle(int position) {
String[] titlesArray = {
"Title 1",
"Title 2",
"Title 3",
};
return titlesArray[position];
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageview = (ImageView) item_view.findViewById(R.id.image_view);
imageview.setImageResource(image_resources[position]);
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
}
我的自定义样式,用于修改PagerTitleStrip的XML元素:
<style name="viepagertitlestrip">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#d3d3d3</item>
<item name="android:textColor">#666666</item>
<item name="android:textSize">12sp</item>
</style>
就个人而言,我发现默认情况下PagerTitleStrip非常难看。我讨厌下一页的标题如何被压到屏幕的一侧。我的图像阵列将有大约20个图像。我基本上希望每个寻呼机标题都是一个小点,我希望它们都能均匀地分布在一个页面上。这可能吗?
答案 0 :(得分:2)
要在您的构建中获得GitHub
项目:
JitPack
存储库添加到构建文件中。在存储库末尾的build.gradle中添加它:
repositories {
// ...
maven { url "https://jitpack.io" }
}
添加依赖项
dependencies {
compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1@aar'
}