android - Fixing the tabs in PagerTabStrip -
i have pagertabstrip setup, tabs don't good. how make tabs normal? (centered in middle - no double lines)
thanks if can lead me solution. find wierd how can't seem find answer question
here xml - pretty plain.
<android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="visible"> <android.support.v4.view.pagertabstrip android:id="@+id/pager_title_strip" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/primary_blue" android:textcolor="#fff" /> </android.support.v4.view.viewpager>
use pagertabstrip
in viewpager
here picture.
you can this.
xml code
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> <android.support.v4.view.pagertabstrip android:id="@+id/pagertab" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="@color/colorprimary" android:paddingbottom="20dp" android:paddingtop="20dp"/> </android.support.v4.view.viewpager> </relativelayout>
activity code
public class mainactivity extends appcompatactivity { // layouts below tab private view view1, view2, view3; private list<view> viewlist; private viewpager viewpager; private pagertabstrip mpagertabstrip; private list<string> titlelist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); inittitle(); pageradapter pageradapter = getadapter(); viewpager.setadapter(pageradapter); } /** * init view */ private void initview() { viewpager = (viewpager) findviewbyid(r.id.viewpager); mpagertabstrip = (pagertabstrip) findviewbyid(r.id.pagertab); // tab indicator color setting mpagertabstrip.settabindicatorcolorresource(r.color.coloraccent); layoutinflater inflater = getlayoutinflater(); view1 = inflater.inflate(r.layout.layout1, null); view2 = inflater.inflate(r.layout.layout2, null); view3 = inflater.inflate(r.layout.layout3, null); // add view viewlist viewlist = new arraylist<view>(); viewlist.add(view1); viewlist.add(view2); viewlist.add(view3); } /** * add title titlelist */ private void inittitle() { titlelist = new arraylist<string>(); titlelist.add("january, 2017"); titlelist.add("february, 2017"); titlelist.add("july, 2017"); } @nonnull private pageradapter getadapter() { return new pageradapter() { @override public boolean isviewfromobject(view arg0, object arg1) { return arg0 == viewlist.get((int) integer.parseint(arg1.tostring())); } @override public int getcount() { return viewlist.size(); } @override public void destroyitem(viewgroup container, int position, object object) { container.removeview(viewlist.get(position)); } @override public object instantiateitem(viewgroup container, int position) { container.addview(viewlist.get(position)); return position; } /** * title * @param pos * @return */ @override public charsequence getpagetitle(int pos) { // title icon setting ,space added before text spannablestringbuilder spannablestringbuilder = new spannablestringbuilder(" " + titlelist.get(pos)); // drawable drawable = getresources().getdrawable(r.mipmap.ic_launcher); drawable.setbounds(0, 0, drawable.getintrinsicwidth(), drawable.getintrinsicheight()); imagespan span = new imagespan(drawable, imagespan.align_baseline); // text color setting foregroundcolorspan fcs = new foregroundcolorspan(color.white); // icon setting // ssb.setspan(span, 0, 1, spannable.span_exclusive_exclusive); // text color setting spannablestringbuilder.setspan(fcs, 1, spannablestringbuilder.length(), spannable.span_exclusive_exclusive); spannablestringbuilder.setspan(new relativesizespan(1.2f), 1, spannablestringbuilder.length(), spannable.span_exclusive_exclusive); return spannablestringbuilder; } }; } }
you need know settabindicatorcolorresource
, getpagetitle in pageradapter
, , these more important.
Comments
Post a Comment