TabLayout 代码布局以及一些小问题

TabLayout是2015发布在Design Support Library中的一个组件。

最近用TabLayout进行代码布局的时候发现一个不大不小的问题。

首先介绍如何对TabLayout进行代码布局:

首先要有一个FragmentActivity:

package com.example.testapp;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * Created by SXT on 2016/3/25.
 */
public class TabViewPager4 extends FragmentActivity{
    private SimpleFragmentAdapter pagerAdapter;

    private ViewPager viewPager;

    private TabLayout tabLayout;

    private LinearLayout linearLayout;

    private Context mContext;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = this;


        linearLayout = new LinearLayout(mContext);
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        tabLayout = new TabLayout(mContext);
        tabLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        viewPager = new ViewPager(this);
        viewPager.setId(View.generateViewId());
        //1.0f weight
        viewPager.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.0f));
        viewPager.setBackgroundColor(Color.WHITE);
        linearLayout.addView(tabLayout);
        linearLayout.addView(viewPager);

        pagerAdapter = new SimpleFragmentAdapter(getSupportFragmentManager(), this);
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
//        tabLayout.post(new Runnable() {
//            @Override
//            public void run() {
//                tabLayout.setupWithViewPager(viewPager);
//            }
//        });
        tabLayout.setSelectedTabIndicatorColor(Color.BLUE);//tabLayout标签条颜色
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);//tabLayout对齐方式,必须将tabMode设置为MODE_FIXED才有效
        tabLayout.setTabMode(TabLayout.MODE_FIXED);//有两种模式,MODE_FIXED表示固定tabs,并同时显示所有的tabs。

//MODE_SCROLLABLE:可滚动tabs,显示一部分tabs,在这个模式下能包含长标签和大量的tabs,最好用于用户不需要直接比较tabs。

 

        setContentView(linearLayout);
    }


}

  FragmentPagerAdapter:

package com.example.testapp;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
 * Created by SXT on 2016/3/25.
 */
public class SimpleFragmentAdapter extends FragmentPagerAdapter {

    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[]{"tab1","tab2","tab3"};
    private Context mContext;

    public SimpleFragmentAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        return PageFragment4.newInstance(position + 1,mContext);
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }
}

  Fragment:

package com.example.testapp;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by SXT on 2016/3/25.
 */
public class PageFragment4 extends Fragment {
    private static final String ARG_PAGE = "ARG_PAGE";
    private static Context mContext = null;
    private int mPage;
    public static PageFragment4 newInstance(int page, Context context){
        mContext = context;
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        PageFragment4 pageFragment = new PageFragment4();
        pageFragment.setArguments(args);
        return pageFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/

        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        TextView textView = new TextView(mContext);
        textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        textView.setGravity(Gravity.CENTER);
        textView.setText("Fragment #" + mPage);
        linearLayout.addView(textView);
        return linearLayout;
    }

}

  完全没有涉及到Xml文件布局,但是在使用代码布局中有一个需要注意的地方,在FragmentActivity中,必须要为viewPager.setId(View.generateViewId());如果没有这句代码那么每个Tab间移动的时候非常缓慢,而且不显示Viewpager中的内容。

另外,在为viewpager setId的时候,如果setId的参数是除零以外的其他数,则会报错(android studio会报错,eclipse不会)。据说这是因为防止Id冲突。为了解决这个问题,有两种方法:

一、使用上述viewPager.setId(View.generateViewId());的方法

二、新建一个id.xml存放id值,用R.id获取

posted @ 2016-03-30 16:05  月出皎兮  阅读(2411)  评论(0编辑  收藏  举报