SHSegmentControl在github上的项目主页地址:https://github.com/7heaven/SHSegmentControl
SHSegmentControl使用简单,在xml布局文件中即可实现配置。比如,segmentcontrol:texts="A|B|C|D"
即为在选项切换条中的文本。
测试代码如下:
activity_main.xml:
<LinearLayout 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"
android:orientation="vertical" > <!-- segmentcontrol:cornerRadius="5dip" 设置边缘切角 -->
<!-- segmentcontrol:horizonGap="20dip" 设置水平宽度 -->
<!-- segmentcontrol:verticalGap="8dip" 设置一个单元的垂直宽度 -->
<!-- segmentcontrol:direction="horizon" 设置一个单元的水平宽度 --> <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_gravity="center_horizontal"
android:textSize="15sp"
segmentcontrol:colors="#0099CC"
segmentcontrol:cornerRadius="10dip"
segmentcontrol:direction="horizon"
segmentcontrol:horizonGap="20dip"
segmentcontrol:texts="A|B|C|D"
segmentcontrol:verticalGap="8dip" /> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="20dp"
android:text="A"
android:textColor="@android:color/holo_red_light"
android:textSize="50sp" /> <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_gravity="center_horizontal"
android:textSize="15sp"
segmentcontrol:colors="@android:color/holo_orange_light"
segmentcontrol:cornerRadius="10dip"
segmentcontrol:direction="vertical"
segmentcontrol:horizonGap="10dip"
segmentcontrol:texts="A|B|C|D"
segmentcontrol:verticalGap="20dip" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="20dp"
android:text="A"
android:textColor="@android:color/holo_green_light"
android:textSize="50sp" /> </LinearLayout>
activity_main.xml
MainActivity.java:
package com.zzw.testsegmentcontrol; import com.sevenheaven.segmentcontrol.SegmentControl;
import com.sevenheaven.segmentcontrol.SegmentControl.OnSegmentControlClickListener; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final TextView textView = (TextView) findViewById(R.id.textView);
final TextView textView1 = (TextView) findViewById(R.id.textView1); SegmentControl segment_control_horizon = (SegmentControl) findViewById(R.id.segment_control_horizon);
SegmentControl segment_control_vertical = (SegmentControl) findViewById(R.id.segment_control_vertical); segment_control_horizon
.setOnSegmentControlClickListener(new OnSegmentControlClickListener() { @Override
public void onSegmentControlClick(int index) {
Log.d("horizon-当前位置", index + "");
switch (index) {
case 0:
textView.setText("A");
break;
case 1:
textView.setText("B");
break;
case 2:
textView.setText("C");
break;
case 3:
textView.setText("D");
break;
}
}
}); segment_control_vertical
.setOnSegmentControlClickListener(new OnSegmentControlClickListener() { @Override
public void onSegmentControlClick(int index) {
Log.d("vertical-当前位置", index + "");
switch (index) {
case 0:
textView1.setText("A");
break;
case 1:
textView1.setText("B");
break;
case 2:
textView1.setText("C");
break;
case 3:
textView1.setText("D");
break;
}
}
});
}
}