您的位置:首页 > 产品设计 > UI/UE

【Android UI】SlidingTabLayout使用小结

2015-06-06 17:23 435 查看
首先来张效果图



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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android layout app