【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在合适的位置添加
4、在Activity中添加代码
5、SamplePagerAdapter.java
6、对SlidingTabLayout添加自定义属性,values目录中添加styleables.xml
7、修改SlidingTabLayout.java
8、SlidingTabLayout、SlidingTabStrip相关方法属性介绍
9、本例中的自定义布局 tab_view.xml
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>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题