/**
* Created by Dash on 2017/11/30.
*
* FrameLayout从左上角放置控件
*/
public class CombineView extends FrameLayout implements View.OnClickListener {
private TextView textView;
private CheckBox checkBox;
private String text;
private boolean checked;
public CombineView(@NonNull Context context) {
super(context);
init();
}
public CombineView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//获取xml里面给的属性值
//第一个参数表示的是命名空间 第二个参数是属性名
text = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "text");
//第三个参数是默认值
checked = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "checked", false);
init();
}
public CombineView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
/**
* 初始化
*/
private void init() {
//把布局挂载到CombineView上...viewGroup root表示是否有挂载的父控件
textView = view.findViewById(R.id.text_desc);
checkBox = view.findViewById(R.id.check_box);
//设置初始的值
textView.setText(text);
checkBox.setChecked(checked);
//设置点击事件
this.setOnClickListener(this);
}
/**
* 对外提供设置文本显示的方法
*/
public void setText(String text){
textView.setText(text);
}
//对外提供设置是否选中
public void setChecked(boolean flag){
checkBox.setChecked(flag);
}
//获取是否选中
public boolean getChecked(){
return checkBox.isChecked();
}
@Override
public void onClick(View view) {
//点击的时候选中未选中
checkBox.setChecked(! checkBox.isChecked());
}
* Created by Dash on 2017/11/30.
*
* FrameLayout从左上角放置控件
*/
public class CombineView extends FrameLayout implements View.OnClickListener {
private TextView textView;
private CheckBox checkBox;
private String text;
private boolean checked;
public CombineView(@NonNull Context context) {
super(context);
init();
}
public CombineView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//获取xml里面给的属性值
//第一个参数表示的是命名空间 第二个参数是属性名
text = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "text");
//第三个参数是默认值
checked = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "checked", false);
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.combine_layout, this);
// 如果挂载到父控件就不需要添加视图 //this.addView(view);
//获取里面的控件textView = view.findViewById(R.id.text_desc);
checkBox = view.findViewById(R.id.check_box);
//设置初始的值
textView.setText(text);
checkBox.setChecked(checked);
//设置点击事件
this.setOnClickListener(this);
}
/**
* 对外提供设置文本显示的方法
*/
public void setText(String text){
textView.setText(text);
}
//对外提供设置是否选中
public void setChecked(boolean flag){
checkBox.setChecked(flag);
}
//获取是否选中
public boolean getChecked(){
return checkBox.isChecked();
}
@Override
public void onClick(View view) {
//点击的时候选中未选中
checkBox.setChecked(! checkBox.isChecked());
}
}
在values 中创建values文件------------------------------attrs.xml文件
<resources>
<!--name="CombineView"以下里面的自定义属性用于哪一个自定义的控件-->
<declare-styleable name="CombineView">
<!--name="text"属性名 format="string" 属性的数据类型-->
<attr name="text" format="string"></attr>
<attr name="checked" format="boolean"/>
</declare-styleable>
</resources>
public Test(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = getContext().obtainStyledAttributes( attrs, R.styleable.Test,defStyleAttr, 0); float dimensionPixelSize = a.getDimensionPixelSize( R.styleable.Test_textg, 0); // 这个测量的字体更精确 R.styleable.Test_textg Test_textg组成是: <declare-styleable name="Test"> //<attr name="textg" format="dimension" ></attr> ///</declare-styleable> float dimension = a.getDimension(R.styleable.Test_textg, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, getResources().getDisplayMetrics())); }