【Android UI】SlidingTabLayout使用小结

转自:https://blog.csdn.net/itiblue/article/details/46390003

首先来张效果图

1、从https://developer.android.com/intl/zh-cn/samples/SlidingTabsBasic/index.html下载项目

2、将SlidingTabLayout.java 和 SlidingTabStrip拷贝到自己项目中

3、Activity对应layout在合适的位置添加

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >    
    <com.gigaset.gsdrive.views.SlidingTabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app1:textViewDefaultColor="#000000"
        app1:textViewFocusColor="#59A9FF"
        android:background="#ffffff"/>
</LinearLayout>   
<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:background="@android:color/white" />


4、在Activity中添加代码

mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(new SamplePagerAdapter());
mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setDividerColors(Color.argb(0, 0, 0, 0));
mSlidingTabLayout.setSelectedIndicatorColors(Color.argb(255, 0x59, 0xA9, 0xFF));//设置滚动条颜色
mSlidingTabLayout.setCustomTabView(R.layout.tab_view, R.id.tabText);
mSlidingTabLayout.setViewPager(mViewPager);


5、SamplePagerAdapter.java

class SamplePagerAdapter extends PagerAdapter {
    @Override
    public int getCount() {
        return tabs.length;
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return object == view;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return tabs[position];
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // Inflate a new layout from our resources
        View view = getLayoutInflater().inflate(R.layout.pager_item, container, false);
        // Add the newly created View to the ViewPager
        container.addView(view);
        // Retrieve a TextView from the inflated View, and update it's text
        TextView title = (TextView) view.findViewById(R.id.item_title);
        title.setText(String.valueOf(position + 1));
        Log.i("cccc", "instantiateItem() [position: " + position + "]");
        // Return the View
        return view;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        Log.i("cccc", "destroyItem() [position: " + position + "]");
    }
 
}


6、对SlidingTabLayout添加自定义属性,values目录中添加styleables.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SlidingTabLayout">
        <attr name="textViewFocusColor" format="color"></attr>
        <attr name="textViewDefaultColor" format="color"></attr>
    </declare-styleable>
</resources>


7、修改SlidingTabLayout.java

public SlidingTabLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.SlidingTabLayout);
    this.focusColor = t.getColor(R.styleable.SlidingTabLayout_textViewFocusColor, Color.BLACK);
    this.defaultColor = t.getColor(R.styleable.SlidingTabLayout_textViewDefaultColor, Color.BLACK);
    t.recycle();</span>
    // Disable the Scroll Bar
    setHorizontalScrollBarEnabled(false);
    // Make sure that the Tab Strips fills this View
    setFillViewport(true);
    mTitleOffset = (int) (TITLE_OFFSET_DIPS * getResources().getDisplayMetrics().density);
    mTabStrip = new SlidingTabStrip(context);
    addView(mTabStrip, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}


8、SlidingTabLayout、SlidingTabStrip相关方法属性介绍

SlidingTabStrip.DEFAULT_BOTTOM_BORDER_THICKNESS_DIPS //修改滑动条高度
SlidingTabStrip.SELECTED_INDICATOR_THICKNESS_DIPS //修改下划线高度
SlidingTabLayout.mSlidingTabLayout.setDividerColors() //设置tab间隔线的颜色
SlidingTabLayout.setSelectedIndicatorColors() //设置滚动条颜色
SlidingTabLayout.setCustomTabView() //自定义tab的布局


9、本例中的自定义布局 tab_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="40dp"
    android:layout_gravity="center" >
    <TextView
        android:id="@+id/tabText"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

--------------------- 
作者:Blue布洛 
来源:CSDN 
原文:https://blog.csdn.net/itiblue/article/details/46390003 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值