标签:
mTabLayout.addTab(mTabLayout.newTab().setText("Tab1"));
mTabLayout.addTab(mTabLayout.newTab().setText("Tab2"));
mTabLayout.addTab(mTabLayout.newTab().setText("Tab3"));
<?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:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp" />
<android.support.design.widget.TabLayout
android:id="@+id/my_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/my_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
mViewPager = (ViewPager) findViewById(R.id.my_viewpager);
mAdapter = new MyAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
public class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
MyFragment fragment = MyFragment.newInstance(position);
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
return "Tab" + position;
}
}
public class MyFragment extends Fragment {
private static final String ARG_POS = "arg_pos";
public MyFragment() {
}
public static MyFragment newInstance(int position) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putInt(ARG_POS, position);
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
TextView textView = (TextView) view.findViewById(R.id.fragment_tv);
textView.setText("Fragment" + getArguments().getInt(ARG_POS));
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="28sp"
android:gravity="center"
android:id="@+id/fragment_tv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
mTabLayout = (TabLayout) findViewById(R.id.my_tablayout);
mTabLayout.setupWithViewPager(mViewPager);
<android.support.design.widget.TabLayout
android:id="@+id/my_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@color/colorPrimary"
app:tabIndicatorColor="@android:color/white"
app:tabSelectedTextColor="#ffff0000"
app:tabTextColor="#ffffffff" />
mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
int count = mTabLayout.getTabCount();
for (int i = 0; i < count; i++) {
mTabLayout.getTabAt(i).setIcon(R.mipmap.ic_launcher);
}
<android.support.design.widget.TabLayout
android:id="@+id/my_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/tab_bg"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="#ffff0000"
app:tabTextColor="#88000000" />
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke
android:color="#88000000"
android:width="1dp" />
</shape>
<android.support.design.widget.TabLayout
android:id="@+id/my_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/tab_bg"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="#2a9ff9"
app:tabTextColor="#88000000" />
private int[] mUnselectDrawable = {R.mipmap.tab_home_unselect,
R.mipmap.tab_speech_unselect, R.mipmap.tab_contact_unselect, R.mipmap.tab_more_unselect};
private int[] mSelectDrawable = {R.mipmap.tab_home_select,
R.mipmap.tab_speech_select, R.mipmap.tab_contact_select, R.mipmap.tab_more_select};
private int mSelectedIndex = 0;
private void setupTabLayout() {int count = mTabLayout.getTabCount();
for (int i = 0; i < count; i++) {
if (i == mSelectedIndex) {
mTabLayout.getTabAt(i).setIcon(mSelectDrawable[i]);
} else {
mTabLayout.getTabAt(i).setIcon(mUnselectDrawable[i]);
}
}
}
mTabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager){
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
tab.setIcon(mSelectDrawable[tab.getPosition()]);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
super.onTabUnselected(tab);
tab.setIcon(mUnselectDrawable[tab.getPosition()]);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
super.onTabReselected(tab);
}
});
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putInt(ARG_SELECT_INDEX, mSelectedIndex);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout);
if (savedInstanceState != null) {
mSelectedIndex = savedInstanceState.getInt(ARG_SELECT_INDEX, 0);
}
initViews();
setupTabLayout();
}
Android Support Design Library 之 TabLayout
标签:
原文地址:http://blog.csdn.net/zwlove5280/article/details/51198773