功能特色
1,类似tab的标签页
2,可以修改左边的文字和右边的文字
3,左边和右边都有相应的点击事件
布局文件
1,注意事项:根标签LinearLayout,主要用于相应的javaBean继承的类
2,类似于按钮的TextView的2种实现
1,宽高给固定的值width="96dp" height="36dp" gravity="center"
2,宽高都是wrap_content,给padding-left,padding-top,padding-right padding-bottom设置相应的值
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#419ED5"
android:gravity="center"
android:paddingBottom="4dp"
android:paddingTop="4dp" >
<TextView
android:id="@+id/view_lock_tv_left"
android:layout_width="93dp"
android:layout_height="36dp"
android:background="@drawable/segament_left_selector"
android:clickable="true"
android:gravity="center"
android:text="未加锁"
android:textColor="@drawable/segament_text_selector" />
<TextView
android:id="@+id/view_lock_tv_right"
android:layout_width="93dp"
android:layout_height="36dp"
android:background="@drawable/segament_right_selector"
android:clickable="true"
android:gravity="center"
android:text="已加锁"
android:textColor="@drawable/segament_text_selector" />
</LinearLayout>
自定义属性的设置
在res/values/attrs.xml文件中,将需要修改的属性暴露出来
注意事项:format中string为小写
1,左边的文本
name="svLeft" format="string|reference"
2,右边的文本
name="svRight" format="string|reference"
3,哪一边被选中,可以使用枚举来进行选择,值为int类型
attr中 name="svSelected"
enum中 name="left" value="0"
enum中 name="right" value="1"
<declare-styleable name="SegamentView">
<attr name="svLeft" format="string|reference"/>
<attr name="svRight" format="string|reference"/>
<attr name="svSelected">
<enum name="left" value="0" />
<enum name="right" value="1" />
</attr>
</declare-styleable>
布局文件对应的javaBean
1,加载布局,注意父控件一定要写this
2,通过上下文过滤属性,记得在后面释放typearray
3,获取属性值
4,获取控件
5,给控件赋值
6,释放typearray
7,处理svSelected属性
1,获取svSelected的属性值
2,使用switch进行判断
3,如果为0,svLeft.setSelected(true),svRight.setSelected(false);
4,如果为1,svLeft.setSelected(false),svRight.setSelected(true);
8,设置类似于RadioGroup的点击事件
1,定义一个设置监听事件的方法,参数传入一个监听的接口
将参数接口对象赋值给成员变量的接口对象
2,定义一个监听的接口
3,给svLeft和svRight设置点击事件
当左边点击的时候
1,设置左边被选择,右边没有被选择
svLeft.setSelected(true),svRight.setSelected(false);
2,调用接口的一个方法listener.onLeftClick();用于显露给用户响应相应的事件
当右边点击的时候
1,设置右边被选择,左边没有被选择
2,调用接口的一个方法listener.onRightClick();用于显示给用户响应相应的事件
注意事项:一定要对监听的接口进行非空的判断,不然会出现空指针
public OnClickChangeListener listener;
private TextView tvLeft;
private TextView tvRight;
public SegamentView(Context context, AttributeSet attrs) {
super(context, attrs);
View.inflate(context, R.layout.view_app_lock, this);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SegamentView);
String strLeft = ta.getString(R.styleable.SegamentView_svLeft);
String strRight = ta.getString(R.styleable.SegamentView_svRight);
int isSelected = ta.getInt(R.styleable.SegamentView_svSelected, 0);
tvLeft = (TextView) findViewById(R.id.view_lock_tv_left);
tvRight = (TextView) findViewById(R.id.view_lock_tv_right);
tvLeft.setText(strLeft);
tvRight.setText(strRight);
tvLeft.setOnClickListener(this);
tvRight.setOnClickListener(this);
switch (isSelected) {
case 0:
tvLeft.setSelected(true);
tvRight.setSelected(false);
break;
case 1:
tvRight.setSelected(true);
tvLeft.setSelected(false);
break;
default:
break;
}
ta.recycle();
}
public void setOnClickChangeListener(OnClickChangeListener listener) {
this.listener = listener;
}
public interface OnClickChangeListener {
void onLeftSelected();
void onRightSelected();
}
public SegamentView(Context context) {
this(context, null);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.view_lock_tv_left:
if (listener != null) {
listener.onLeftSelected();
tvLeft.setSelected(true);
tvRight.setSelected(false);
}
break;
case R.id.view_lock_tv_right:
if (listener != null) {
listener.onRightSelected();
tvLeft.setSelected(false);
tvRight.setSelected(true);
break;
}
default:
break;
}
}
组合控件在布局文件中的使用
注意事项
1,命令空间,前面android可以改为app,后面android改为必须是自己应用的包名
布局文件中的根标签的命令空间
xmlns:app="http://schemas.android.com/apk/res/com.example.safeguard1"
<com.example.view.SegamentView
android:id="@+id/act_applock_sv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:svLeft="未加锁"
app:svRight="已加锁"
app:svSelected="left" >
</com.example.view.SegamentView>
组合控件在Activity中的使用
1,初始化控件
sv = (SegamentView) findViewById(R.id.act_applock_sv);
2,设置监听事件
sv.setOnClickChangeListener(this);
3,重写接口中暴露出来的方法
1,可以用来设置文本
2,可以用来设置不一样的适配器
@Override
public void onLeftSelected() {
tvTitle.setText("未加锁(" + listUnlock.size() + ")个");
isLeft = true;
AppLockAdapter adapter = new AppLockAdapter(listUnlock, this, isLeft, listLock,tvTitle);
lv.setAdapter(adapter);
}
@Override
public void onRightSelected() {
isLeft = false;
tvTitle.setText("已加锁(" + listLock.size() + ")个");
AppLockAdapter adapter = new AppLockAdapter(listLock, this, isLeft, listUnlock,tvTitle);
lv.setAdapter(adapter);
}