public class ComBineView extends FrameLayout {
private String text;
private boolean isck;
public ComBineView(@NonNull Context context) {
super(context);
init();
}
public ComBineView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
text = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "text");
isck = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "ck", true);
init();
}
public ComBineView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
//把布局挂载到CombineView上...viewGroup root表示是否有挂载的父控件
View view = View.inflate(getContext(), R.layout.combineview_layout, this);
TextView textView= view.findViewById(R.id.text);
CheckBox ck= view.findViewById(R.id.ck);
textView.setText(text);
ck.setChecked(isck);
}
}
attrs
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ComBineView">
<attr name="text" format="string"></attr>
<attr name="ck" format="boolean"></attr>
</declare-styleable>
</resources>
在ComBineView 中提供对外的传值得方法可以对自定义控件进行改值