MainActivity
/**
* 设置style 1.manifest中设置theme 2.style api11 3.图片
* 4.适配器中的方法 getPageTitle(int position)
*/
public class MainActivity extends FragmentActivity {
private ViewPager vp;
private TabPageIndicator tabPageIndicator;
private List<Fragment> list = new ArrayList<Fragment>();
String titles[] = { "推荐", "军事", "情感", "法治", "生活", "运动" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
preparefragment();
vp.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),
list, titles));
tabPageIndicator.setViewPager(vp);
}
private void preparefragment() {
for (int i = 0; i < titles.length; i++) {
Myfragment myfragment = new Myfragment();
Bundle args = new Bundle();
args.putString("url", titles[i]);
myfragment.setArguments(args);
list.add(myfragment);
}
}
private void initView() {
tabPageIndicator = (TabPageIndicator) findViewById(R.id.tabpageindicator);
vp = (ViewPager) findViewById(R.id.vp);
}
}
fragment
import com.example.day04_viewpagerindicator.R;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Myfragment extends Fragment{
private View view;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_view, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView tv_fragment = (TextView)view.findViewById(R.id.tv_fragment);
Bundle bundle = getArguments();
String string = bundle.getString("url");
tv_fragment.setText(string);
}
}
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
List<Fragment> list;
String[] titles;
public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list, String[] titles) {
super(fm);
this.list=list;
this.titles=titles;
}
@Override
public Fragment getItem(int arg0) {
return list.get(arg0);
}
@Override
public int getCount() {
return list.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
manifest
android:theme="@style/StyledIndicators"
Styles
<style name="StyledIndicators" parent="@android:style/Theme.Light">
<item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item>
</style>
<style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator">
<item name="android:background">@drawable/tab_indicator</item>
<item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item>
<item name="android:textSize">14sp</item>
<item name="android:dividerPadding" tools:targetApi="11">8dp</item>
<item name="android:showDividers" tools:targetApi="11">middle</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:fadingEdge">horizontal</item>
<item name="android:fadingEdgeLength">8dp</item>
</style>
<!
<style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
<item name="android:typeface">monospace</item>
<item name="android:textColor">@drawable/selector_tabtext</item>
</style>
drawable–selector_tabtext.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:state_selected="false" android:state_pressed="true" android:drawable="@android:color/transparent" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/base_tabpager_indicator_selected" />
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/base_tabpager_indicator_selected" />
</selector>
drawable–tab_indicator.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:state_selected="false" android:state_pressed="true" android:drawable="@android:color/transparent" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/base_tabpager_indicator_selected" />
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/base_tabpager_indicator_selected" />
</selector>
drawable
![这里写图片描述](https://img-blog.csdn.net/20161016204654188)