MasterofProject

Design Support Android control introduced TabLayout

47 people read comment(0) Collection report
Classification:
Then on a Design Support Library [Android commonly used controls (on)] (http://prog3.com/sbdm/blog/true100/article/details/50593636) this article to briefly introduce the use of TabLayout.
Usually when we do with the title of the slide effect, are used to achieve TabPageIndicator+ViewPager. Today we use TabLayout+ViewPager to achieve the same effect, but also to achieve a much simpler way than before. Let's go straight into the code!

Main interface layout

   <? Version= XML "1" 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 set the text color -- > <!
    <! Text color when -- > --tabSelectedTextColor
    <! --tabIndicatorColor indicates the color of life
    < -- > --tabIndicatorHeight indicating the height!
    <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> 

Use Fragment page simple layout

<? Version= XML "1" 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="World Hello!">
< /RelativeLayout>

Frament class

Public Class TestFragment Extends Fragment{
    PrivateContent String;
    PrivateContent_tv TextView;
    PrivateFragmentView View;

    @Override
    Public Void OnCreate(Bundle savedInstanceState @Nullable) {
        Super.onCreate (savedInstanceState);
Content= (String) getArguments ().Get () ()"Content");
}

    @Override
    PublicViewOnCreateView(inflater ViewGroup, container LayoutInflater, savedInstanceState Bundle) {
Inflater.inflate fragmentView= (R.layout.content_main,Null);
Content_tv= (TextView) fragmentView.findViewById (R.id.content_tv);
Content_tv.setText (content);
        ReturnFragmentView;
}
}

ViewPager adapter class

/ * *
* by: ${ldm} Created
* time: 15:34 2016/1/27
* /
Public Class TestAdapter Extends FragmentPagerAdapter{
    PrivateFragments List<Fragment>;
    PrivateList<String>titles;
    Public TestAdapter(FM List<String>titles, FragmentManager, fragments List<Fragment>) {
        Super(FM);
        This.fragments=fragments;
        This.titles=titles;
}

    / * *
* the Fragment associated with a specified Return position.
*
*@paramPosition
* /
    @Override
    PublicFragmentGetItem(IntPosition) {
        ReturnFragments.get (position);
}

    / * *
* the number of views available. Return
* /
    @Override
    Public Int GetCount(1)
        ReturnFragments.size ();
}

    @Override
    PublicCharSequenceGetPageTitle(IntPosition) {
        ReturnTitles.get (position);
}
}

Home page code class

Public Class MainActivity Extends FragmentActivity{
    Convenient to realize display label / /
    PrivateTab_layout TabLayout;
    PrivateViewpager ViewPager;
    Privatetestadapter madapter;
    私人片段>片段;
    私人列表<字符串>标题;

    “重写”
    受保护的 无效 onCreate(束savedinstancestate){
        好极了onCreate(savedinstancestate);
setContentView(r.layout。activity_main);
initviews();
initviewpages();
}

    私人 无效 initviewpages(){
片段=新的ArrayList <>();
头衔=新的ArrayList <>();
        对于int我=我<我+ +){
字符串标题=“标签”+我;
标题(标题);
tab_layout。addtab(tab_layout。newtab()。setText(标题));/ /添加选项卡标题
片段片段新的testfragment();
束束=新的bundle();
束。putString(“内容”“这是第”+我+“个片段页面”);
setarguments(束)的片段;
片段(片段);
}

madapter =新的testadapter(getsupportfragmentmanager()、标题、片段);
setadapter ViewPager(madapter);
tab_layout setupwithviewpager(ViewPager);
tab_layout settabsfrompageradapter(madapter);
}

    私人 无效 initviews(){
tab_layout =(tablayout)findViewById(r.id.tab_layout);
ViewPager =(ViewPager)findViewById(r.id.viewpager);
}

}

效果如图:
Write the picture here.

猜你在找
查看评论
*以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:48748次
    • 积分:二千二百五十八
    • 等级:
    • 排名:10193名第
    • 原创:183篇
    • 转载:37篇
    • 译文:1篇
    • 评论:13条
    文章分类
    文章存档
    最新评论