1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class CustomTitleView extends View implements View.OnClickListener{ private String titleText; private int titleTextColor; private int titleTextSize; private Rect rect; private Paint paint; public CustomTitleView(Context context) {this(context, null);} public CustomTitleView(Context context, AttributeSet attrs) {this(context, attrs, 0);} public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyleAttr, 0); int n = ta.getIndexCount(); for(int i = 0; i < n; i++){ int attr = ta.getIndex(i); switch(attr){ case R.styleable.CustomTitleView_titleText: titleText = ta.getString(attr); break; case R.styleable.CustomTitleView_titleTextColor: titleTextColor = ta.getColor(attr, Color.BLACK); break; case R.styleable.CustomTitleView_titleTextSize: titleTextSize = ta.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics())); break; } } ta.recycle(); paint = new Paint(); paint.setTextSize(titleTextSize); rect = new Rect(); paint.getTextBounds(titleText, 0, titleText.length(), rect); } }
|