Created
July 23, 2015 23:18
-
-
Save lilac/69c1c4ca2e0da4f07654 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OptionLayout extends LinearLayout | |
{ | |
@InjectView(R.id.title) | |
TextView title; | |
@InjectView(R.id.summary) | |
TextView summary; | |
@InjectView(R.id.content) | |
ViewGroup content; | |
private Paint paint = new Paint(); | |
public OptionLayout(Context context) | |
{ | |
this(context, null); | |
} | |
public OptionLayout(Context context, AttributeSet attrs) | |
{ | |
this(context, attrs, R.attr.optionLayoutStyle); | |
} | |
public OptionLayout(Context context, AttributeSet attrs, int defStyleAttr) | |
{ | |
super(context, attrs, defStyleAttr); | |
init(context, attrs, defStyleAttr); | |
setWillNotDraw(false); | |
} | |
private void init(Context context, AttributeSet attrs, int defStyleAttr) | |
{ | |
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OptionLayout, defStyleAttr, 0); | |
String titleAttr = a.getString(R.styleable.OptionLayout_android_title); | |
String summaryAttr = a.getString(R.styleable.OptionLayout_android_summary); | |
inflate(getContext(), R.layout.option_layout, this); | |
ButterKnife.inject(this); | |
title.setText(titleAttr); | |
summary.setText(summaryAttr); | |
a.recycle(); | |
} | |
public void setTitle(String title) | |
{ | |
this.title.setText(title); | |
} | |
@Override | |
public void addView(@NonNull final View child, int index, ViewGroup.LayoutParams params) | |
{ | |
if (content == null) | |
{ | |
super.addView(child, index, params); | |
} | |
else | |
{ | |
content.addView(child, index, params); | |
// Propagate click event to the last child view. | |
setOnClickListener(new OnClickListener() | |
{ | |
@Override | |
public void onClick(View v) | |
{ | |
child.performClick(); | |
} | |
}); | |
} | |
} | |
@Override | |
protected void onDraw(Canvas canvas) | |
{ | |
super.onDraw(canvas); | |
paint.setColor(android.graphics.Color.RED); | |
paint.setStrokeWidth(20); | |
canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, | |
this.getHeight() - 1, paint); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment