【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的布局
<?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
- 【Android UI】SlidingTabLayout使用小结
- Android进阶UI之SlidingTabLayout实现ViewPager页卡滑动效果
- SlidingTabLayout
- Android UI 小结
- android UI小结(一)
- android UI小结(二)
- android UI小结(三)
- android UI小结(四)
- android UI小结(五)
- android UI小结(六)
- Android UI优化小结
- 使用SlidingTabLayout实现仿头条标题切换
- com.flyco.tablayout.SlidingTabLayout的使用
- 使用jQuery UI Datepicker 小结
- Android UI布局和事件处理小结
- android UI布局的性能优化小结
- Android开发小结——UI开发
- android SlidingTabLayout实现ViewPager页卡滑动效果
- springMVC bootstarp 用户权限基础框架+即时通讯
- 最长单调递增子序列——动态规划
- 使用pkg-config升级和切换glib库
- Core Graphics图形变换
- pythonanywhere下django无法匹配url问题解决
- 【Android UI】SlidingTabLayout使用小结
- ArcGIS API for JavaScript 需要在DoJo中加载的资源
- 聚类算法实践(1)——层次、K-means聚类
- JOGL测试
- bind query.c
- text
- 项目
- 《编程珠玑》阅读小记(3) — 数据决定数据结构
- C Primer Plus 第三章