value中创建对应的attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="VideoItemView">
<attr name="is_need_cover_layout" format="boolean"/>
<attr name="cover_layout_padding_left" format="reference|dimension"/>
<attr name="cover_layout_padding_right" format="reference|dimension"/>
<attr name="is_need_top_bar" format="boolean"/>
<attr name="is_need_default_progress_bar" format="boolean"/>
<attr name="is_need_play_progress_bar" format="boolean"/>
<attr name="is_fullscreen" format="boolean"/>
</declare-styleable>
</resources>
java代码中初始化相关的代码并获取attr值
public VideoItemView(Context context) {
this(context, null);
}
public VideoItemView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public VideoItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
if (IS_DEBUG) {
Log.i(TAG, "init()");
}
initViews(context, attrs);
}
private void initViews(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.VideoItemView);
mIsNeedCoverLayout = ta.getBoolean(R.styleable.VideoItemView_is_need_cover_layout, false);
mIsNeedTopBar = ta.getBoolean(R.styleable.VideoItemView_is_need_top_bar, false);
mIsNeedDefaultProgressBar = ta.getBoolean(R.styleable.VideoItemView_is_need_default_progress_bar, false);
mIsNeedPlayProgressBar = ta.getBoolean(R.styleable.VideoItemView_is_need_play_progress_bar, true);
mIsFullscreen = ta.getBoolean(R.styleable.VideoItemView_is_fullscreen, false);
mCoverLayoutPaddingLeft = (int) ta.getDimension(R.styleable.VideoItemView_cover_layout_padding_left, 0);
mCoverLayoutPaddingRight = (int) ta.getDimension(R.styleable.VideoItemView_cover_layout_padding_right, 0);
ta.recycle();
mRootView = (RelativeLayout) View.inflate(context, R.layout.item_video_root_layout, this);
if (mIsNeedCoverLayout) {
initVideoCoverViews();
} else {
initAddPlayView();
}
}