android_自定义布局

1.需要实现view类
2.如果需要实现自定义属性则:
1.定义资源文件attrs---->values
2.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="rect_color" format="color"/>
</declare-styleable>
</resources>


3.如果要设置默认的值

public MyRect(Context context, AttributeSet attrs) {
super(context, attrs);

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView);

int color = ta.getColor(R.styleable.MyView_rect_color, 0xffff0000);
setBackgroundColor(color);

ta.recycle();
}

4.如果想在布局文件中使用,则需要声明

xmlns:jkxy="http://schemas.android.com/apk/res/com.example.myview(包名)" 

然后就可以使用jkxy:属性名称="值"

Hold on, everything is possible.
原文地址:https://www.cnblogs.com/student-note/p/6116798.html