你的位置:首页 > 操作系统

[操作系统]选项切换条


SHSegmentControl在github上的项目主页地址:https://github.com/7heaven/SHSegmentControl 

SHSegmentControl使用简单,在即为在选项切换条中的文本。

测试代码如下:

activity_main.

 1 <LinearLayout ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/tools" 3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   android:orientation="vertical" > 6  7   <!-- segmentcontrol:cornerRadius="5dip" 设置边缘切角 --> 8   <!-- segmentcontrol:horizonGap="20dip" 设置水平宽度 --> 9   <!-- segmentcontrol:verticalGap="8dip" 设置一个单元的垂直宽度 -->10   <!-- segmentcontrol:direction="horizon" 设置一个单元的水平宽度 -->11 12   <com.sevenheaven.segmentcontrol.SegmentControl13     ="http://schemas.android.com/apk/res-auto"14     android:id="@+id/segment_control_horizon"15     android:layout_width="wrap_content"16     android:layout_height="wrap_content"17     android:layout_gravity="center_horizontal"18     android:textSize="15sp"19     segmentcontrol:colors="#0099CC"20     segmentcontrol:cornerRadius="10dip"21     segmentcontrol:direction="horizon"22     segmentcontrol:horizonGap="20dip"23     segmentcontrol:texts="A|B|C|D"24     segmentcontrol:verticalGap="8dip" />25 26   <TextView27     android:id="@+id/textView"28     android:layout_width="wrap_content"29     android:layout_height="wrap_content"30     android:layout_gravity="center_horizontal"31     android:paddingTop="20dp"32     android:text="A"33     android:textColor="@android:color/holo_red_light"34     android:textSize="50sp" />35   36   37   <com.sevenheaven.segmentcontrol.SegmentControl38     ="http://schemas.android.com/apk/res-auto"39     android:id="@+id/segment_control_vertical"40     android:layout_width="wrap_content"41     android:layout_height="wrap_content"42     android:layout_gravity="center_horizontal"43     android:textSize="15sp"44     segmentcontrol:colors="@android:color/holo_orange_light"45     segmentcontrol:cornerRadius="10dip"46     segmentcontrol:direction="vertical"47     segmentcontrol:horizonGap="10dip"48     segmentcontrol:texts="A|B|C|D"49     segmentcontrol:verticalGap="20dip" />50   51   <TextView52     android:id="@+id/textView1"53     android:layout_width="wrap_content"54     android:layout_height="wrap_content"55     android:layout_gravity="center_horizontal"56     android:paddingTop="20dp"57     android:text="A"58     android:textColor="@android:color/holo_green_light"59     android:textSize="50sp" />60 61 </LinearLayout>

activity_main.

MainActivity.java:

 1 package com.zzw.testsegmentcontrol; 2  3 import com.sevenheaven.segmentcontrol.SegmentControl; 4 import com.sevenheaven.segmentcontrol.SegmentControl.OnSegmentControlClickListener; 5  6 import android.app.Activity; 7 import android.os.Bundle; 8 import android.util.Log; 9 import android.widget.TextView;10 11 public class MainActivity extends Activity {12 13   @Override14   protected void onCreate(Bundle savedInstanceState) {15     super.onCreate(savedInstanceState);16     setContentView(R.layout.activity_main);17 18     final TextView textView = (TextView) findViewById(R.id.textView);19     final TextView textView1 = (TextView) findViewById(R.id.textView1);20     21     SegmentControl segment_control_horizon = (SegmentControl) findViewById(R.id.segment_control_horizon);22     SegmentControl segment_control_vertical = (SegmentControl) findViewById(R.id.segment_control_vertical);23     24     segment_control_horizon25         .setOnSegmentControlClickListener(new OnSegmentControlClickListener() {26 27           @Override28           public void onSegmentControlClick(int index) {29             Log.d("horizon-当前位置", index + "");30             switch (index) {31             case 0:32               textView.setText("A");33               break;34             case 1:35               textView.setText("B");36               break;37             case 2:38               textView.setText("C");39               break;40             case 3:41               textView.setText("D");42               break;43             }44           }45         });46     47     segment_control_vertical48     .setOnSegmentControlClickListener(new OnSegmentControlClickListener() {49 50       @Override51       public void onSegmentControlClick(int index) {52         Log.d("vertical-当前位置", index + "");53         switch (index) {54         case 0:55           textView1.setText("A");56           break;57         case 1:58           textView1.setText("B");59           break;60         case 2:61           textView1.setText("C");62           break;63         case 3:64           textView1.setText("D");65           break;66         }67       }68     });69   }70 }