您的位置:首页 > 移动开发 > Android开发

Android Design Support控件介绍之TabLayout

2016-01-27 16:36 417 查看
接着上一篇[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);
}

}


效果如图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: