RadioGroup实现类似ios的分段选择(UISegmentedControl)控件

文章来源:http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0512/1615.html

在ios7中有一种扁平风格的控件叫做分段选择控件UISegmentedControl,控件分为一排,横放着几个被简单线条隔开的按钮,每次点击只能选择其中一个按钮,他类似于tabbar但是又稍微有点区别,新版的qq手机客户端就用到了这种控件。

但是在android中并没有现成的控件可用,不过android中有着功能类似但UI相差很大的RadioGroup控件,可以通过定义RadioGroup的外观来达到相同的目的。其实android中也没有TabBar,但是很多app通过修改RadioGroup来实现ios中的UITabBar效果,看来RadioGroup是还真是很实用的控件,虽然原生的真的很丑。

新手对RadioGroup的自定义可能很难下手,git上有了一个现成的封装好了的库以及例子,可以下载学习,如果你是用eclipse开发的项目,可能需要改改才能用,因为他提供的是android studio的项目结构。

项目地址:https://github.com/hoang8f/android-segmented-control

使用方法:

1.先将库文件作为library倒入到eclipse,在你的项目中引用这个lib。

2.然后在需要分段控件的activity 布局文件中加入xml代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<info.hoang8f.android.segmented.SegmentedGroup
     android:id= "@+id/segmented4"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:layout_margin= "10dp"
     android:orientation= "horizontal" >
     <RadioButton
         android:id= "@+id/in_month"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "本月"
         style= "@style/RadioButton" />
     <RadioButton
         android:id= "@+id/in_year"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "今年"
         style= "@style/RadioButton" />             
                                                                                                                                                                                                                                             
</info.hoang8f.android.segmented.SegmentedGroup>


style="@style/RadioButton"可以不用管,因为库文件中已经定义了这个style。

android:id="@+id/in_year"android:id="@+id/in_month"的id用于代码中判断哪个按钮处于选中状态。

3.在activity中使用SegmentedGroup并添加选择事件:


1
2
3
SegmentedGroup segmented4 = (SegmentedGroup)findViewById(R.id.segmented4);
segmented4.setTintColor(0xFF1b7ce8);
segmented4.setOnCheckedChangeListener( this );

segmented4.setTintColor(0xFF1b7ce8);设置分段选择按钮的颜色。

segmented4.setOnCheckedChangeListener( this );将分段选择的事件监听设为本activity,接下来实现监听事件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
     switch (checkedId) {
         case R.id.in_month:
             mPager.setAdapter(mAdapter);
             mPager.setCurrentItem(MAX_COUNT/2);
             mPager.setOffscreenPageLimit(5);
             return ;
         case R.id.in_year:
             mPager.setAdapter(mAdapter1);
             mPager.setCurrentItem(MAX_COUNT/2);
             mPager.setOffscreenPageLimit(5);
             return ;
     }
}


如果你已经知道如何运用了不妨花点时间研究它是如何实现的,其实非常简单,不过是重新定义了RadioGroup 子元素的背景而已,在android中darwable背景本身就带有两种状态,选中和未选中。如果更改了带有两种状态的Drawable也就完全更改了radiogroup的外观。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private void updateBackground() {
     int count = super .getChildCount();
     LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     params.setMargins(0, 0, -oneDP, 0);
     if (count > 1) {
         super .getChildAt(0).setLayoutParams(params);
         updateBackground(getChildAt(0), R.drawable.radio_checked_left, R.drawable.radio_unchecked_left);
         for (int i = 1; i < count - 1; i++) {
             updateBackground(getChildAt(i), R.drawable.radio_checked_middle, R.drawable.radio_unchecked_middle);
             super .getChildAt(i).setLayoutParams(params);
         }
         updateBackground(getChildAt(count - 1), R.drawable.radio_checked_right, R.drawable.radio_unchecked_right);
     } else if (count == 1) {
         updateBackground(getChildAt(0), R.drawable.radio_checked_default, R.drawable.radio_unchecked_default);
     }
}
private void updateBackground(View view, int checked, int unchecked) {
     //Set text color
     ColorStateList colorStateList = new ColorStateList( new int[][]{
             {android.R.attr.state_pressed},
             {-android.R.attr.state_pressed, -android.R.attr.state_checked},
             {-android.R.attr.state_pressed, android.R.attr.state_checked}},
             new int[]{Color.GRAY, mTintColor, mCheckedTextColor});
     ((Button) view).setTextColor(colorStateList);
     //Redraw with tint color
     Drawable checkedDrawable = resources.getDrawable(checked);
     Drawable uncheckedDrawable = resources.getDrawable(unchecked);
     ((GradientDrawable) checkedDrawable).setColor(mTintColor);
     ((GradientDrawable) uncheckedDrawable).setStroke(oneDP, mTintColor);
     //Create drawable
     StateListDrawable stateListDrawable = new StateListDrawable();
     stateListDrawable.addState( new int[]{-android.R.attr.state_checked}, uncheckedDrawable);
     stateListDrawable.addState( new int[]{android.R.attr.state_checked}, checkedDrawable);
     //Set button background
     view.setBackgroundDrawable(stateListDrawable);
}

可自定义样式、功能全面的分段控件。项目地址:https://github.com/klongmitre/android-segmented-control-view效果图:如何使用xml中直接创建<org.mitre.ascv.AndroidSegmentedControlView         android:id="@ id/androidSegmentedControlView"         android:layout_width="match_parent"         android:layout_height="wrap_content"         ascv:ascv_defaultSelection="0"         ascv:ascv_unselectedTextColor="@color/test_attr_unselected_text_color"         ascv:ascv_selectedTextColor="@color/test_attr_selected_text_color"         ascv:ascv_selectedColor="@color/test_attr_selected_color"         ascv:ascv_unselectedColor="@color/test_attr_unselected_color"         ascv:ascv_items="@array/three_state_option"/>2. java中添加监听器,监听item的切换ascv = (AndroidSegmentedControlView)this.findViewById(R.id.androidSegmentedControlView); ascv.setIdentifier("ascv01"); //ascv.setItems(new String[]{"Test1aaaaa", "Test2", "Test3"}, new String[]{"1", "2", "3"}); ascv.setOnSelectionChangedListener(new OnSelectionChangedListener(){     @Override     public void newSelection(String identifier, String value) {//当item切换时触发  Toast.makeText(MainActivity.this, "identifier:" identifier "  value:" value, Toast.LENGTH_SHORT).show();     } });参数identifier是当有多个分段控件时,同时使用一个监听器时,用于区别是哪个触发了事件。属性说明属性名类型使用说明ascv_unselectedTextColorreference未选中的item的文字颜色ascv_unselectedColorreference未选中的item的背景颜色,不包括边框ascv_selectedColorreference选中的item背景的颜色以及边框的颜色ascv_selectedTextColorreference选中的item的文字颜色ascv_itemsreference控件item上显示的文字ascv_valuesreference控件item的值,会被传给监听器。未设置时,默认使用ascv_items。ascv_equalWidthboolean当item上的文字,即ascv_items设置的文字,长度不一致时,item的宽度是否还等长。ascv_stretchboolean是否被拉伸。即控件是否填充整个父容器。ascv_defaultSelectioninteger默认哪个item被选中,下标从0开始ascv_identifierstring控件的ID
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值