Android-Tab单选控件
今天看到项目中有一个控件写得很美丽,据说是github上开源的控件,地址没找到,例如以下图所看到的,很常见的效果,几个tab页面来回切换:
转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/46799341
FlatTabGroup.java:
public class FlatTabGroup extends RadioGroup implements RadioGroup.OnCheckedChangeListener {
public FlatTabGroup(Context context) {
this(context, null);
}
private int mRadius;
private int mStroke;
private int mHighlightColor;
private String[] mItemString;
private float mTextSize;
private ColorStateList mTextColor;
private int[] mTabViewIds;
private OnTabCheckedListener mTabCheckedListener;
public FlatTabGroup(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(HORIZONTAL);
setGravity(Gravity.CENTER_VERTICAL);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.FlatTabGroup);
mHighlightColor = array.getColor(R.styleable.FlatTabGroup_tab_border_color, Color.WHITE);
mStroke = array.getDimensionPixelSize(R.styleable.FlatTabGroup_tab_border_width, 2);
mRadius = array.getDimensionPixelOffset(R.styleable.FlatTabGroup_tab_radius, 5);
mTextColor = array.getColorStateList(R.styleable.FlatTabGroup_tab_textColor);
mTextSize = array.getDimensionPixelSize(R.styleable.FlatTabGroup_tab_textSize, 14);
int id = array.getResourceId(R.styleable.FlatTabGroup_tab_items, 0);
array.recycle();
mItemString = isInEditMode() ? new String[] { "TAB A", "TAB B", "TAB C"} : context.getResources().getStringArray(id);
generateTabView(context, attrs);
super.setOnCheckedChangeListener(this);
}
private void generateTabView(Context context, AttributeSet attrs) {
if (mItemString == null) {
return;
}
mTabViewIds = new int[mItemString.length];
for (int i=0; i<mItemString.length;i++ ) {
<span style="white-space:pre"> </span>String text = mItemString[i];
RadioButton button = new RadioButton(context, attrs);
button.setGravity(Gravity.CENTER);
button.setButtonDrawable(android.R.color.transparent);
button.setText(text);
button.setTextColor(mTextColor);
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
button.setId(mTabViewIds[i] = generateViewIds());
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
if(i < mItemString.length - 1){
<span style="white-space:pre"> </span>lp.rightMargin = -1 * mStroke;
}
addView(button, lp);
}
}
public void setOnTabCheckedListener(OnTabCheckedListener listener) {
mTabCheckedListener = listener;
}
public void setSelection(int position) {
check(getChildAt(position).getId());
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (mTabCheckedListener != null) {
int checkedPosition = -1;
for (int i = 0; i < mTabViewIds.length; i++) {
if (mTabViewIds[i] == checkedId) {
checkedPosition = i;
break;
}
}
mTabCheckedListener.onChecked(this, checkedPosition);
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
updateChildBackground();
}
@SuppressWarnings("deprecation")
private void updateChildBackground() {
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child instanceof RadioButton) {
child.setBackgroundDrawable(generateTabBackground(i, mHighlightColor));
}
}
}
private Drawable generateTabBackground(int position, int color) {//怎样用代码定义selector
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] {android.R.attr.state_checked}, generateDrawable(position, color));
stateListDrawable.addState(new int[] { }, generateDrawable(position, Color.TRANSPARENT));
return stateListDrawable;
}
private Drawable generateDrawable(int position, int color) {
float[] radius;
if (position == 0) {
radius = new float[] {
mRadius, mRadius,
0, 0,
0, 0,
mRadius, mRadius
};
} else if (position == getChildCount() - 1) {
radius = new float[] {
0, 0,
mRadius, mRadius,
mRadius, mRadius,
0, 0
};
} else {
radius = new float[] {
0, 0,
0, 0,
0, 0,
0, 0
};
}
GradientDrawable shape = new GradientDrawable();//怎样用代码生成圆角shape
shape.setCornerRadii(radius);
shape.setColor(color);
shape.setStroke(mStroke, mHighlightColor);
return shape;
}
private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);//怎样使用自己定义的id
/**
* Generate a value suitable for use in {@link #setId(int)}. This value will
* not collide with ID values generated at build time by aapt for R.id.
*
* @return a generated ID value
*/
public static int generateViewIds() {
for (;;) {
final int result = sNextGeneratedId.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range
// under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF)
newValue = 1; // Roll over to 1, not 0.
if (sNextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
}
public static interface OnTabCheckedListener {
public void onChecked(FlatTabGroup group, int position);
}
}
这里有几个点挺有意思:
(1)怎样用代码做selector
(2)怎样用代码生成圆角
(3)怎样使用自己定义的id
attr_flat_tab_group.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FlatTabGroup">
<attr name="tab_items" format="reference" />
<attr name="tab_border_width" format="dimension|reference" />
<attr name="tab_border_color" format="color|reference" />
<attr name="tab_radius" format="dimension|reference" />
<attr name="tab_textColor" format="reference" />
<attr name="tab_textSize" format="dimension|reference" />
</declare-styleable>
</resources>
arrays_flat_tab_group.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="array_tabs_more_video">
<item>实时直播</item>
<item>直播预约</item>
<item>精彩回看</item>
</string-array>
</resources>
引用的时候:
<com.example.view.FlatTabGroup
android:id="@+id/flat_tab_group"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="center_vertical"
app:tab_border_color="#cdcdcd"
app:tab_border_width="1dp"
app:tab_items="@array/array_tabs_more_video"
app:tab_radius="5dp"
app:tab_textColor="@android:color/black"
app:tab_textSize="16sp"/>
FlatTabGroup tabs = (FlatTabGroup)this.findViewById(R.id.flat_tab_group);
tabs.setOnTabCheckedListener(new OnTabCheckedListener(){
@Override
public void onChecked(FlatTabGroup group, int position) {
Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_SHORT).show();
}
});
tabs.setSelection(0);
Android-Tab单选控件的更多相关文章
- Android 中常见控件的介绍和使用
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
- 【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用
Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16: ...
- 介绍三个Android支持库控件:TabLayout+ViewPager+RecyclerView
本文主要介绍如下三个Android支持库控件的配合使用: TabLayout:android.support.design.widget.TabLayout ViewPager:android.sup ...
- Android 一个日历控件的实现代码
转载 2017-05-19 作者:Othershe 我要评论 本篇文章主要介绍了Android 一个日历控件的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看 ...
- Android笔记---常用控件以及用法
这篇文章主要记录下Android的常用控件以及使用的方法,Android 给我们提供了大量的UI控件,合理地使用这些控件就可以非常轻松地编写出相当不错的界面,这些是Android学习的基础,没有什么业 ...
- Android Design Support控件之DrawerLayout简单使用
DrawerLayout能够让我们在项目中非常方便地实现側滑菜单效果.如今主流的应用如QQ等都 採用的这样的效果. 这两天也是在学习Android Design Support的相关知识.网上有关这方 ...
- Android中ListView控件的使用
Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...
- MFC编程 | tab control控件的使用
因为课程需要,会用到MFC编程,所以讲一些经验总结下,以便日后使用查询. // tab control控件的使用 // 建立一个Cluster窗口,通过tab可以切换成C-Means和Fuzzy C- ...
- 五、Android学习第四天补充——Android的常用控件(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...
- 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式
注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式 这个坑,必须要注意呀, 比如在用ListView的时候,如果在List_ ...
随机推荐
- 在C#中调用Win32函数EnumWindows枚举所有窗口。
原文 http://www.cnblogs.com/mfm11111/archive/2009/06/30/1514322.html 开发旺旺群发软件,难点及重要技术点分析(一) 一. ...
- OpenFileDialog 害人的RestoreDirectory
莫名其妙出现找不到文件的错误.经查,发现: OpenFileDialog,SaveFileDialog在选择文件后,会切换当前程序目录的路径(System.Environment.CurrentDir ...
- linux学习方法之六
相信不少想学习linux的新手们正愁不知道看什么linux学习教程好,下面小编给大家收集和整理了几点比较重要的教程,供大家学习,如需想学习更多的话,可到wdlinux学堂寻找更多教程. 1.linux ...
- Javascript base64加密 解密
var base64encodechars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ...
- nginx配置方法
nginx配置的代码: user www www; worker_processes 8; error_log /data111/logs/nginx/nginx-error.log crit; pi ...
- Java导出excel并下载功能
我们使用的导出并下载功能是利用一个插件叫POI的插件提供的导出功能,很实用:首先先导入Jar包: Jar包下载地址:http://poi.apache.org/ 官方文档地址:http://poi ...
- hdu 5570 balls(期望好题)
Problem Description There are n balls with m colors. The possibility of that the color of the i-th b ...
- php算法之快速排序
/** * 快速排序 * 原理: * 快速排序使用分治法(Divide and conquer)策略来把一个串行(list)分为两个子串行(sub-lists). * 最差时间复杂度 O(n*n) * ...
- Cocoapod安装使用
近期换工作.项目中推荐使用Cocoapod,把经常使用的第三方库增加到project中,大致说一下流程,标记一下. 第一步:安装 CocoaPods是跑在Ruby的软件,安装可能须要几分钟.打开你的终 ...
- JavaScript中模块“写法”
在JavaScript模块到底是什么 event = function() { // do more return { bind: function() {}, unbind: function() ...