Android选项切换条SHSegmentControl



Android选项切换条SHSegmentControl

SHSegmentControl是github上一个开源的选项切换条,其样式如图所示:


SHSegmentControl在github上的项目主页地址:https://github.com/7heaven/SHSegmentControl
SHSegmentControl使用简单,在xml布局文件中即可实现配置。比如,segmentcontrol:texts="A|B|C|D"
即为在选项切换条中的文本。
segmentcontrol:verticalGap和segmentcontrol:horizonGap设置选项切换条的高度和其中每一个条目的平均宽度。
通过SHSegmentControl的setOnSegmentControlClickListener(new  OnSegmentControlClickListener ()),为控件增加点击选择触发事件。在回调中,index是从左到右或者自上而下的索引。
现在给出代码实现本文图中所示结果。
布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.sevenheaven.segmentcontrol.SegmentControl
        xmlns:segmentcontrol="http://schemas.android.com/apk/res-auto"
        android:id="@+id/segment_control_horizon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textSize="15sp"
        segmentcontrol:colors="#0099CC"
        segmentcontrol:cornerRadius="5dip"
        segmentcontrol:direction="horizon"
        segmentcontrol:horizonGap="20dip"
        segmentcontrol:texts="A|B|C|D"
        segmentcontrol:verticalGap="8dip" />

    <com.sevenheaven.segmentcontrol.SegmentControl
        xmlns:segmentcontrol="http://schemas.android.com/apk/res-auto"
        android:id="@+id/segment_control_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="15sp"
        segmentcontrol:colors="#0099CC"
        segmentcontrol:cornerRadius="5dip"
        segmentcontrol:direction="vertical"
        segmentcontrol:horizonGap="30dip"
        segmentcontrol:texts="E|F|G|H"
        segmentcontrol:verticalGap="10dip" />

</RelativeLayout>


Java控制代码:

import com.sevenheaven.segmentcontrol.SegmentControl;
import com.sevenheaven.segmentcontrol.SegmentControl.OnSegmentControlClickListener;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        SegmentControl segment_control_horizon=(SegmentControl) findViewById(R.id.segment_control_horizon);
        segment_control_horizon.setOnSegmentControlClickListener(new  OnSegmentControlClickListener (){

			@Override
			public void onSegmentControlClick(int index) {
				Toast.makeText(getApplicationContext(), index+"", Toast.LENGTH_SHORT).show();
			}});
        
        SegmentControl segment_control_vertical=(SegmentControl) findViewById(R.id.segment_control_vertical);
        segment_control_vertical.setOnSegmentControlClickListener(new  OnSegmentControlClickListener (){

			@Override
			public void onSegmentControlClick(int index) {
				Toast.makeText(getApplicationContext(), index+"", Toast.LENGTH_SHORT).show();
			}});
    }
}

转载于:https://my.oschina.net/zhangphil/blog/1601904

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可自定义样式、功能全面的分段控件。项目地址: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、付费专栏及课程。

余额充值