安卓开发tablayoutt能用于电视开发吗

Android 导航条效果实现(六) TabLayout+ViewPager+Fragment
时间: 20:31:57
&&&& 阅读:5535
&&&& 评论:
&&&& 收藏:0
标签:TabLayout
一、继承结构
public class TabLayout
extends HorizontalScrollView
java.lang.Object
android.view.View
android.view.ViewGroup
&&&&&&&&&&&&?
android.widget.FrameLayout
&&&&&&&&&&&&&&&&?
android.widget.HorizontalScrollView
&&&&&&&&&&&&&&&&&&&&?
android.support.design.widget.TabLayout
二、TabLayout的使用
1、TabLayout简单使用
TabLayout来自design兼容包,使用需要添加依赖。android studio 添加依赖如下:
dependencies {
compile ‘com.android.support:appcompat-v7:23.4.0‘
compile ‘com.android.support:design:24.2.0‘
TabLayout可以水平放置一些tab标签
标签的展示通过 TabLayout.Tab的实例来实现,通过 newTab()方法可以创建这些示例,并且可以通过 setText(int)和setIcon(int)方法分别设置标签的文本的图标。 另外,需要通过layout的setTab(Tab)方法把一个标签展示出来。例如:
tabLayout = (TabLayout)findViewById(R.id.tablayout)
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"))
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"))
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"))
你需要通过setOnTabSelectedListener(OnTabSelectedListener)方法,来给标签的选择改变设定一个监听器。当选择状态改变的时候,会通知你的监听器。
你在布局中也可以通过使用TabItem标签给TabLayout设置子标签,例如:
&android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"&
&android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标签" /&
&android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@mipmap/ic_launcher" /&
&/android.support.design.widget.TabLayout&
效果如下:
ViewPager+TabLayout实现导航效果
目标效果一:
如果你将TabLayout与ViewPager一起使用,你可以通过调用setupWithViewPager(ViewPager)方法来绑定绑定两个控件。TabLayout将自动出现在ViewPager的标题位置。
方法如下:
MainActivity.java
package com.noonecode.
import android.support.design.widget.TabL
import android.support.v4.app.F
import android.support.v4.app.FragmentM
import android.support.v4.app.FragmentPagerA
import android.support.v4.view.ViewP
import android.support.v7.app.AppCompatA
import android.os.B
import java.util.ArrayL
import java.util.L
public class MainActivity extends AppCompatActivity {
private TabLayout tabL
private ViewPager viewP
private List&Fragment&
private MyA
private String[] titles = {"页面1", "页面2", "页面3"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
list = new ArrayList&&();
list.add(new Tab1Fragment());
list.add(new Tab2Fragment());
list.add(new Tab3Fragment());
adapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
public Fragment getItem(int position) {
return list.get(position);
public int getCount() {
return list.size();
public CharSequence getPageTitle(int position) {
return titles[position];
activity_main.java
&?xml version="1.0" encoding="utf-8"?&
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.noonecode.tablayoutdemo.MainActivity"&
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" /&
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"&
TabLayout也支持当作ViewPager的装饰来使用,通过在布局中直接把它添加到ViewPager的内部(将自动将ViewPager的标题作为TabLayout的Tab)。
如下方式使用:
MainActivity.java
package com.noonecode.
import android.support.design.widget.TabL
import android.support.v4.app.F
import android.support.v4.app.FragmentM
import android.support.v4.app.FragmentPagerA
import android.support.v4.view.ViewP
import android.support.v7.app.AppCompatA
import android.os.B
import java.util.ArrayL
import java.util.L
public class MainActivity extends AppCompatActivity {
private ViewPager viewP
private List&Fragment&
private MyA
private String[] titles = {"页面1", "页面2", "页面3"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
list = new ArrayList&&();
list.add(new Tab1Fragment());
list.add(new Tab2Fragment());
list.add(new Tab3Fragment());
adapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
class MyAdapter extends FragmentPagerAdapter {
public CharSequence getPageTitle(int position) {
return titles[position];
activity_main.java
&?xml version="1.0" encoding="utf-8"?&
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.noonecode.tablayoutdemo.MainActivity"&
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"&
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" /&
目标效果二
实现方法:
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
其他可定制属性:
XML attributes
android.support.design:tabBackground
Reference to a background to be applied to tabs.
android.support.design:tabContentStart
Position in the Y axis from the starting edge that tabs should be positioned from.
android.support.design:tabGravity
Gravity constant for tabs.
android.support.design:tabIndicatorColor
Color of the indicator used to show the currently selected tab.
android.support.design:tabIndicatorHeight
Height of the indicator used to show the currently selected tab.
android.support.design:tabMaxWidth
The maximum width for tabs.
android.support.design:tabMinWidth
The minimum width for tabs.
android.support.design:tabMode
The behavior mode for the Tabs in this layoutMust be one of the following constant values.
android.support.design:tabPadding
The preferred padding along all edges of tabs.
android.support.design:tabPaddingBottom
The preferred padding along the bottom edge of tabs.
android.support.design:tabPaddingEnd
The preferred padding along the end edge of tabs.
android.support.design:tabPaddingStart
The preferred padding along the start edge of tabs.
android.support.design:tabPaddingTop
The preferred padding along the top edge of tabs.
android.support.design:tabSelectedTextColor
The text color to be applied to the currently selected tab.
android.support.design:tabTextAppearance
A reference to a TextAppearance style to be applied to tabs.
android.support.design:tabTextColor
The default text color to be applied to tabs.
GRAVITY_CENTER
Gravity used to lay out the tabs in the center of the TabLayout.
GRAVITY_FILL
Gravity used to fill the TabLayout as much as possible.
MODE_FIXED
Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.
MODE_SCROLLABLE
Scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs.
目标效果三:
实现方法:
TabLayout默认可以实现图示的效果的,比较简单,这里讲述,如何自定义效果。
可以自定义每个Tab的的布局样式,实现丰富的效果。如下:
tab_custom.xml
&?xml version="1.0" encoding="utf-8"?&
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"&
android:id="@+id/iv_icon"
android:layout_width="30dp"
android:layout_height="30dp" /&
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /&
activity_main.xml
这里需要把指示条隐藏掉:tabIndicatorHeight="0dp"
&?xml version="1.0" encoding="utf-8"?&
xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.noonecode.tablayoutdemo.MainActivity"&
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/&
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="60dp"
app:tabSelectedTextColor="#59D"
app:tabTextColor="#999"
app:tabIndicatorHeight="0dp" /&
MainActivity.java
package com.noonecode.
import android.content.C
import android.support.design.widget.TabL
import android.support.v4.app.F
import android.support.v4.app.FragmentM
import android.support.v4.app.FragmentPagerA
import android.support.v4.view.ViewP
import android.support.v7.app.AppCompatA
import android.os.B
import android.view.LayoutI
import android.view.V
import android.widget.ImageV
import android.widget.TextV
import java.util.ArrayL
import java.util.L
public class MainActivity extends AppCompatActivity {
private TabLayout tabL
private ViewPager viewP
private List&Fragment&
private MyA
private String[] titles = {"主页", "群组", "搜索", "消息", "更多"};
private int images[] = {R.drawable.home_selector, R.drawable.group_selector, R.drawable.square_selector, R.drawable.message_selector, R.drawable.more_selector};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
list = new ArrayList&&();
list.add(new Tab1Fragment());
list.add(new Tab2Fragment());
list.add(new Tab3Fragment());
list.add(new Tab4Fragment());
list.add(new Tab5Fragment());
adapter = new MyAdapter(getSupportFragmentManager(), this);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i & tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(adapter.getTabView(i));
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm, Context context) {
super(fm);
this.context =
public Fragment getItem(int position) {
return list.get(position);
public int getCount() {
return list.size();
* 自定义方法,提供自定义Tab
* position 位置
* 返回Tab的View
public View getTabView(int position) {
View v = LayoutInflater.from(context).inflate(R.layout.tab_custom, null);
TextView textView = (TextView) v.findViewById(R.id.tv_title);
ImageView imageView = (ImageView) v.findViewById(R.id.iv_icon);
textView.setText(titles[position]);
imageView.setImageResource(images[position]);
textView.setTextColor(tabLayout.getTabTextColors());
一些资源:
home_selector.xml,其他的类似
&?xml version="1.0" encoding="utf-8"?&
xmlns:android="/apk/res/android"&
android:drawable="@mipmap/icon_home_sel" android:state_selected="true" /&
android:drawable="@mipmap/icon_home_nor" /&
图片资源:
TabLayout是5.0出来后的design兼容包内的,如要使用,请手动添加依赖。
本例使用Android Studio导包只需要添加添加依赖,将自动下载项目。
绑定ViewPager之后,将自动将ViewPager的标题作为Tab标签,需要重写适配器的getPageTitle方法给其设置标题。这时,通过addTab添加的Tab将是无效的。
AppCompatActivity继承自FragmentActivity,小伙伴们继承自FragmentActivity也是一样使用的。
如有问题请留言。
Android 导航条效果实现(一) TabActivity+TabHost
Android 导航条效果实现(二) FragmentTabHost
Android 导航条效果实现(三) ViewPager+PagerTabStrip
Android 导航条效果实现(四) ViewPager+自定义导航条
Android 导航条效果实现(五) ActionBar+Fragment
Android 导航条效果实现(六) TabLayout+ViewPager+Fragment
&&国之画&&&& &&&&chrome插件
版权所有 京ICP备号-2
迷上了代码!1962人阅读
Android(54)
TabLayout是用来实现tab导航的控件,放在android.support.design.widget包里面,使用它可以很简单有很完美的结合ViewPager或者FragmentManager实现tab导航,并且实现与ViewPager的联动等功能。 本文主要介绍了:
TabLayout的基本特性。
如何与ViewPager联动。
如何自定义TabLayout的显示。
2. 基本使用和常见属性
android:id="@+id/fragment_discover_tably"
android:layout_width="match_parent"
android:layout_height="wrap_content"/&
1. tabMode
app:tabMode="fixed|scrollable"
tabMode属性有两个取值:
fixed时,所有的tab会居中显示,是不可滑动的,
scrollable时,所有的tab会靠左显示,当tab数量很多时,就会呈现滑动的效果,这对适配小屏幕手机至关重要。
2. 导航条的高度和颜色
app:tabIndicatorHeight="3dp"
app:tabIndicatorColor="#af00"
3. tab的宽度限制
当文字很少时仍旧不会小于tabMinWidth
当文字过多时,也不会将tab撑的超过tabMaxWidth
app:tabMaxWidth="150dp"
app:tabMinWidth="60dp"
4. tab被选中或没有被选中时的颜色
app:tabTextColor="@color/black"
app:tabSelectedTextColor="@color/colorPrimary"
5. 更改Tab上面的TextView的详细显示
app:tabTextAppearance="@style/TabLayoutStyle"
name="TabLayoutStyle" parent="TextAppearance.Design.Tab"&
& =":textSize"&16&/&
& =":textStyle"&&/&
app:tabBackground="@color/transparent"
设置tab的背景颜色,默认TabLayout点击时是有光晕效果的,使用该属性可以去掉光晕效果。
3. 与ViewPager联动
使用方法tabLy.setupWithViewPager(viewPager);,就可以了,但是需要注意的是写ViewPager时需要实现getTitle()方法,用来设置tab的标题。原先给Tab设置的标题将会被覆盖掉。
4. 自定义Tab显示
默认的TabLayout是可以显示文字和Icon的,定制度不是很高,有时候很难达到想要的效果,不过好在TabLayout开放了setCustomView(childView)这个API来支持我们自定义TabLayout的UI,当然自定义显示之后也损失了切换时颜色切换等效果,需要我们自己来处理,这边我模仿adapter的方式写了一个辅助类,用来更简单的实现自定义Tab显示时的显示和状态切换,源码很简单,主要是使用模板方法的方式,由 子类决定 并且 只关注 如何加载数据和更改状态显示的操作。
* Describe : TabLayout适配器,快速实现TabLayout自定义View数据加载
* chendong
public abstract class BaseTabLayoutAdapter&T& {
private Context
private int
private List&T&
private List&TabLayoutViewHolder& mTabLayoutViewH
private TabLayout
private OnTabSelectListener&T&
mOnTabSelectL
* Tab选中的接口
public interface OnTabSelectListener&T& {
void onSelect(int pos, T data);
protected BaseTabLayoutAdapter(Context context, List&T& datas, int resId) {
mContext =
mResId = resId;
mTabLayoutViewHolders = new ArrayList&&();
public void attachTabLayout(TabLayout tabLayout) {
attachTabLayout(tabLayout, 0);
private void attachTabLayout(TabLayout tabLayout, int posSelect) {
mTabLayout = tabL
tabLayout.setSelectedTabIndicatorHeight(0);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
updateStatus(mTabLayoutViewHolders.get(position), mDatas.get(position), true);
if (mOnTabSelectListener != null) {
mOnTabSelectListener.onSelect(position, mDatas.get(position));
public void onTabUnselected(TabLayout.Tab tab) {
updateStatus(mTabLayoutViewHolders.get(tab.getPosition()), mDatas.get(tab.getPosition()), false);
public void onTabReselected(TabLayout.Tab tab) {
updateStatus(mTabLayoutViewHolders.get(tab.getPosition()), mDatas.get(tab.getPosition()), true);
View childV
TabLayoutViewH
TabLayout.T
for (int i = 0; i & mDatas.size(); i++) {
childView = LayoutInflater.from(mContext).inflate(mResId, tabLayout, false);
holder = new TabLayoutViewHolder(childView);
mTabLayoutViewHolders.add(holder);
updateStatus(mTabLayoutViewHolders.get(i), mDatas.get(i), false);
tab = tabLayout.newTab().setCustomView(childView);
tabLayout.addTab(tab);
TabLayout.Tab tabAt = tabLayout.getTabAt(posSelect);
if (tabAt != null)
tabAt.select();
* 抽象方法,更新控件状态显示
* holder view holder
* data 数据
* isSelect 是否选中
public abstract void updateStatus(TabLayoutViewHolder holder, T data, boolean isSelect);
public void setOnTabSelectListener(OnTabSelectListener&T& onTabSelectListener) {
mOnTabSelectListener = onTabSelectL
* 公共holder,用来存储和快速获取控件
public static class TabLayoutViewHolder {
private View
private SparseArray&View& mCacheV
public View getParentView() {
return parentV
TabLayoutViewHolder(View parentView) {
this.parentView = parentV
mCacheViews = new SparseArray&&();
public &V extends View& V getView(int resId) {
V v = (V) mCacheViews.get(resId);
if (v == null) {
v = (V) parentView.findViewById(resId);
if (v != null) {
mCacheViews.put(resId, v);
BaseTabLayoutAdapter&BeautifyToolTabData& baseTabLayoutAdapter =
new BaseTabLayoutAdapter&ModelData&(getContext(), tabDatas, R.layout.kiwi_item_beautify_tably) {
public void updateStatus(TabLayoutViewHolder holder, ModelData data, boolean isSelect) {
holder.&TextView&getView(R.id.tv_title).setText(data.getTitle());
holder.&ImageView&getView(R.id.iv_icon).setImageResource(isSelect ? data.getSelectRes() : data.getUnSelectRes());
baseTabLayoutAdapter.setOnTabSelectListener(new BaseTabLayoutAdapter.OnTabSelectListener&ModelData&() {
public void onSelect(int pos, ModelData data) {
baseTabLayoutAdapter.attachTabLayout(mTabLayout);
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:66978次
积分:1490
积分:1490
排名:千里之外
原创:78篇
评论:32条
(1)(1)(5)(1)(2)(6)(6)(6)(6)(11)(1)(2)(2)(11)(11)(5)(1)关于TabLayout的使用 ,自定义了一个框架。。。 以后写底部菜单就可以直接作为依赖库 ,不用麻烦了
来源:博客园
首先,简单的说一下,这个底部框架的实现步骤。 1,自定义一个类,继承LinearLayout就好了 ,因为我喜欢用LinearLayout, 当然你也可以继承RelativeLayout。 2,重写它两个带参数的构造方法。 3,进入内容的编写,布局文件的处理,数据的处理。 4, 给底部容器添加view,底部菜单实现的一个页面切换。 好了,由于个人的表诉能力 。直接上代码好了 package com.weight.tabbottomlib. import java.util.ArrayL import android.content.C import android.graphics.C import android.util.AttributeS import android.view.LayoutI import android.view.V import android.widget.LinearL import android.widget.TextV /** * 底部切换框架 * * @author Administrator * */ public class MyBottomLayout extends LinearLayout { static ArrayList itemData = new ArrayList(); static ArrayList viewArryItem = new ArrayList(); LayoutI // 布局管理器 View mainV // 这个是底部的容器 View viewI int itemSize = 0; public MyBottomLayout(Context context, AttributeSet attrs) { super(context, attrs); } /** * 给底部的容器添加view * * @param view */ public void addBottomLayoutValue(ArrayList itemData) { LayoutInflater inflater = LayoutInflater.from(getContext()); this.itemData = itemD this.itemSize = itemData.size(); for (int i = 0; i < itemData.size(); i++) { View viewItem = inflater.inflate(R.layout.layout_tab_item, null); viewItem.findViewById(R.id.tabImg).setBackgroundResource( itemData.get(i).getDrawableNormalId()); TextView textView = ((TextView) (viewItem .findViewById(R.id.tabText))); textView.setText(itemData.get(i).getName()); textView.setTextColor(Color.WHITE); this.addView(viewItem, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f)); viewItem.setId(i); viewArryItem.add(viewItem); viewItem.setOnClickListener(new lister()); } // 处理一下整理的数据 changeDataState(); } /** * 改变一下那个数据的状态 */ private void changeDataState() { if (itemSize != 5) { // 不需要处理 for (int i = itemS i < 5; i++) { itemData.add(new Item()); LayoutInflater inflater = LayoutInflater.from(getContext()); View viewItem = inflater .inflate(R.layout.layout_tab_item, null); viewArryItem.add(viewItem); } } } public interface ICallbackLister { public void click(int id); } ICallbackLister callbackLister = public void setOnCallbackLister(ICallbackLister callbackLister) { this.callbackLister = callbackL } /** * * @ClassName: lister * @Description:tab点击事件的处理 * @author lumin * @date
下午12:53:36 * */ private class lister implements OnClickListener { @Override public void onClick(View clickView) { callbackLister.click(clickView.getId()); initBottom(clickView.getId()); } } public static void initBottom(int id) { switch (id) { case 0: // 第二步:实现页面的一个切换 initData(itemData.get(0).getDrawableSelectId(), itemData.get(1) .getDrawableNormalId(), itemData.get(2) .getDrawableNormalId(), itemData.get(3) .getDrawableNormalId(), itemData.get(4) .getDrawableNormalId()); changeTextColor( (TextView) (viewArryItem.get(0).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(1).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(2).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(3).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(4).findViewById(R.id.tabText))); case 1: initData(itemData.get(0).getDrawableNormalId(), itemData.get(1) .getDrawableSelectId(), itemData.get(2) .getDrawableNormalId(), itemData.get(3) .getDrawableNormalId(), itemData.get(4) .getDrawableNormalId()); changeTextColor( (TextView) (viewArryItem.get(1).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(0).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(2).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(3).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(4).findViewById(R.id.tabText))); case 2: initData(itemData.get(0).getDrawableNormalId(), itemData.get(1) .getDrawableNormalId(), itemData.get(2) .getDrawableSelectId(), itemData.get(3) .getDrawableNormalId(), itemData.get(4) .getDrawableNormalId()); changeTextColor( (TextView) (viewArryItem.get(2).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(0).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(1).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(3).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(4).findViewById(R.id.tabText))); case 3: initData(itemData.get(0).getDrawableNormalId(), itemData.get(1) .getDrawableNormalId(), itemData.get(2) .getDrawableNormalId(), itemData.get(3) .getDrawableSelectId(), itemData.get(4) .getDrawableNormalId()); changeTextColor( (TextView) (viewArryItem.get(3).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(0).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(1).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(2).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(4).findViewById(R.id.tabText))); case 4: initData(itemData.get(0).getDrawableNormalId(), itemData.get(1) .getDrawableNormalId(), itemData.get(2) .getDrawableNormalId(), itemData.get(3) .getDrawableNormalId(), itemData.get(4) .getDrawableSelectId()); changeTextColor( (TextView) (viewArryItem.get(4).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(0).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(1).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(2).findViewById(R.id.tabText)), (TextView) (viewArryItem.get(3).findViewById(R.id.tabText))); } } /** * * @param resIdOne * @param resIdTwo * @param resIdThree * @param resIdFour * @param resIdFive */ public static void initData(int resIdOne, int resIdTwo, int resIdThree, int resIdFour, int resIdFive) { try { viewArryItem.get(0).findViewById(R.id.tabImg) .setBackgroundResource(resIdOne); viewArryItem.get(1).findViewById(R.id.tabImg) .setBackgroundResource(resIdTwo); viewArryItem.get(2).findViewById(R.id.tabImg) .setBackgroundResource(resIdThree); viewArryItem.get(3).findViewById(R.id.tabImg) .setBackgroundResource(resIdFour); viewArryItem.get(4).findViewById(R.id.tabImg) .setBackgroundResource(resIdFive); } catch (Exception err) { err.printStackTrace(); // 报错的时候说明已经没有那么多的item了 } } /** * * @param txtOne *
这个就是默认的需要改变的图片的颜色 * @param txtTwo * @param txtThree * @param txtFour * @param txtFive */ public static void changeTextColor(TextView txtOne, TextView txtTwo, TextView txtThree, TextView txtFour, TextView txtFive) { txtOne.setTextColor(Color.MAGENTA); txtTwo.setTextColor(Color.BLUE); txtThree.setTextColor(Color.BLUE); txtFour.setTextColor(Color.BLUE); txtFive.setTextColor(Color.BLUE); } } 好了 ,这样就完成了框架的编写, 缺少了什么昵 。。。 下面就给出当前页面的item
。。 和实体。。 首先是布局文件
接着是。。实体文件 package com.weight.tabbottomlib. public class Item { private int drawableNormalId; private int drawableSelectId; private S public Item(int drawableNormalId, int drawableSelectId, String name) { super(); this.drawableNormalId = drawableNormalId; this.drawableSelectId = drawableSelectId; this.name = } public Item() { super(); } public int getDrawableNormalId() { return drawableNormalId; } public void setDrawableNormalId(int drawableNormalId) { this.drawableNormalId = drawableNormalId; } public int getDrawableSelectId() { return drawableSelectId; } public void setDrawableSelectId(int drawableSelectId) { this.drawableSelectId = drawableSelectId; } public String getName() { } public void setName(String name) { this.name = } } 这样就全部实现了。使用的话 要作为一个库的哦,右键你的工程
点击Build Path 下的Config Build Path,进入Android,选择isLibrary
点击ok,就完成了。 然后具体的使用方法,接下来的文章里 我会告诉大家的。
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动}

我要回帖

更多关于 tablayout fragment 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信