【Android UI】SlidingTabLayout使用小结

来源:互联网 发布:网络用语棒子什么意思 编辑:程序博客网 时间:2024/05/29 04:50

首先来张效果图



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>







1 0