Android Design Support控件介绍之TabLayout

接着上一篇[Android Design Support Library常用控件介绍(上)](http://blog.csdn.net/true100/article/details/50593636)这篇来简单介绍下TabLayout的使用。
  通常我们在做带有标题的左右滑动效果时,都使用的是TabPageIndicator+ViewPager来实现。今天我们使用TabLayout+ViewPager来实现同样的效果,而且实现方法比之前的简单得多。我们直接进入代码吧!

主界面布局

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">
    <!--tabTextColor设置文字颜色-->
    <!--tabSelectedTextColor点中时的文字颜色-->
    <!--tabIndicatorColor指示条的颜色-->
    <!--tabIndicatorHeight指示条的高度-->
    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="@android:color/holo_green_dark"
        app:tabIndicatorHeight="4dp"
        app:tabSelectedTextColor="@android:color/holo_red_dark"
        app:tabTextColor="@android:color/black"></android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout> 

用到的Fragment页面简单布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <TextView
        android:id="@+id/content_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

Frament类

public class TestFragment extends Fragment {
    private String content;
    private TextView content_tv;
    private View fragmentView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        content=(String)getArguments().get("content");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        fragmentView= inflater.inflate(R.layout.content_main,null);
        content_tv=(TextView)fragmentView.findViewById(R.id.content_tv);
        content_tv.setText(content);
        return fragmentView;
    }
}

ViewPager适配器类

/**
 * Created by: ${ldm}
 * time : 2016/1/27 15:34
 */
public class TestAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;
    private List<String>titles;
    public TestAdapter(FragmentManager fm,List<String>titles,List<Fragment> fragments) {
        super(fm);
        this.fragments=fragments;
        this.titles=titles;
    }

    /**
     * Return the Fragment associated with a specified position.
     *
     * @param position
     */
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    /**
     * Return the number of views available.
     */
    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}

主页面代码类

public class MainActivity extends FragmentActivity {
    //便捷实现标签显示
    private TabLayout tab_layout;
    private ViewPager viewpager;
    private TestAdapter mAdapter;
    private List<Fragment> fragments;
    private List<String> titles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initViewPages();
    }

    private void initViewPages() {
        fragments = new ArrayList<>();
        titles = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            String title="TAB" + i;
            titles.add(title);
            tab_layout.addTab(tab_layout.newTab().setText(title));//添加Tab标题
            Fragment fragment = new TestFragment();
            Bundle bundle = new Bundle();
            bundle.putString("content", "这是第" + i + "个Fragment页面");
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }

        mAdapter = new TestAdapter(this.getSupportFragmentManager(), titles, fragments);
        viewpager.setAdapter(mAdapter);
        tab_layout.setupWithViewPager(viewpager);
        tab_layout.setTabsFromPagerAdapter(mAdapter);
    }

    private void initViews() {
        tab_layout = (TabLayout) findViewById(R.id.tab_layout);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
    }

}

效果如图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值