这是我的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张图像。我基本上希望每个寻呼机标题都带有一个小点,并且我希望它们全部均匀地分布在一页上。这可能吗?
最佳答案
要将GitHub
项目放入构建中:
将JitPack
存储库添加到您的构建文件中。
将其添加到存储库末尾的build.gradle中:
repositories {
// ...
maven { url "https://jitpack.io" }
}
添加依赖项
dependencies {
compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1@aar'
}