首先下个第三方的库Library
先写清单文件:里面写
在xml文件里写: application:
android:theme="@style/StyledIndicators"
在res/values/styles/写以下xml文件:
<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/里面有3个文件设置背景颜色:
selector_tabtext.xml里面的写的内容如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 被选中的颜色 --> <item android:state_selected="true" android:color="#EE2C2C" /> <!-- 按下的颜色 --> <item android:state_pressed="true" android:color="#EE2C2C" /> <!-- 焦点 --> <item android:state_focused="true" android:color="#EE2C2C" /> <item android:color="@android:color/black"/> </selector> tab_indicator.xml里面的内容如下:<selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- state_selected:被选中 state_pressed:按下 --> <!-- 没有选中,也没有按下时,颜色为透明 --> <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>
mainxml文件里面写以下布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bawei.zhishiqilianxi1.MainActivity" > <com.viewpagerindicator.TabPageIndicator android:id="@+id/indicator" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
在代码里面写指示器和ViewPager进行关联
public class MainActivity extends FragmentActivity{ String list[] = { "热点", "美文", "视频", "旅游", "汽车", "体育", "财经", "娱乐", "精选","热点", "美文", "视频", "旅游", "汽车", "体育", "财经", "娱乐", "精选", "文化" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
//找指示器id TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator); //找viewpager的id
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); FragmentManager supportFragmentManager = getSupportFragmentManager(); viewPager.setAdapter(new MyPagerAdapter(supportFragmentManager,list)); //设置指示器和viewPager关联
indicator.setViewPager(viewPager); } } 在MyPagerAdapter里面
public class MyPagerAdapter extends FragmentPagerAdapter { private String[] list; public MyPagerAdapter(FragmentManager fm, String[] list) { super(fm); // TODO Auto-generated constructor stub this.list = list; } @Override public Fragment getItem(int arg0) { MyFragment myFragment = new MyFragment(); Bundle bundle = new Bundle(); bundle.putString("name", list[arg0]); myFragment.setArguments(bundle); return myFragment; } @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return list[position]; } @Override public int getCount() { // TODO Auto-generated method stub return list.length; } }图片放到drawable_hdpi里面:
其余就是在HomeFragement 写展示的东西了
下面是用了一个TabHost