|
| 1 | +package com.example.customview; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.util.AttributeSet; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.widget.LinearLayout; |
| 8 | +import android.widget.TextView; |
| 9 | + |
| 10 | +public class LovelyView extends LinearLayout { |
| 11 | + private String leftLabel = ""; |
| 12 | + private String rightLabel = ""; |
| 13 | + private TextView leftTextView; |
| 14 | + private TextView rightTextView; |
| 15 | + private int leftStyle ; |
| 16 | + private int rightStyle; |
| 17 | + |
| 18 | + |
| 19 | + public LovelyView(Context context) { |
| 20 | + super(context); |
| 21 | + LayoutInflater.from(context).inflate(R.layout.key_value_layout, this); |
| 22 | + } |
| 23 | + |
| 24 | + public LovelyView(Context context, AttributeSet attrs) { |
| 25 | + super(context, attrs); |
| 26 | + initViews(context, attrs); |
| 27 | + } |
| 28 | + |
| 29 | + public LovelyView(Context context, AttributeSet attrs, int defStyle) { |
| 30 | + this(context, attrs); |
| 31 | + initViews(context, attrs); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + private void initViews(Context context, AttributeSet attrs) { |
| 37 | + TypedArray a = context.getTheme().obtainStyledAttributes(attrs, |
| 38 | + R.styleable.LovelyView, 0, 0); |
| 39 | + |
| 40 | + try { |
| 41 | + // get the text and colors specified using the names in attrs.xml |
| 42 | + leftLabel = a.getString(R.styleable.LovelyView_leftLabel); |
| 43 | + rightLabel = a.getString(R.styleable.LovelyView_rightLabel); |
| 44 | + leftStyle = a.getResourceId(R.styleable.LovelyView_leftLabelStyle, android.R.style.TextAppearance_DeviceDefault); |
| 45 | + rightStyle = a.getResourceId(R.styleable.LovelyView_rightLabelStyle, android.R.style.TextAppearance_DeviceDefault); |
| 46 | + |
| 47 | + } finally { |
| 48 | + a.recycle(); |
| 49 | + } |
| 50 | + |
| 51 | + LayoutInflater.from(context).inflate(R.layout.key_value_layout, this); |
| 52 | + |
| 53 | + //left text view |
| 54 | + leftTextView = (TextView) this.findViewById(R.id.leftTextView); |
| 55 | + leftTextView.setText(leftLabel); |
| 56 | + leftTextView.setTextAppearance(context, leftStyle); |
| 57 | + |
| 58 | + //right text view |
| 59 | + rightTextView = (TextView) this.findViewById(R.id.rightTextView); |
| 60 | + rightTextView.setText(rightLabel); |
| 61 | + rightTextView.setTextAppearance(context, rightStyle); |
| 62 | + } |
| 63 | + |
| 64 | + public String getLeftLabel() { |
| 65 | + return leftLabel; |
| 66 | + } |
| 67 | + |
| 68 | + public void setLeftLabel(String leftLabel) { |
| 69 | + this.leftLabel = leftLabel; |
| 70 | + if(leftTextView!=null){ |
| 71 | + leftTextView.setText(leftLabel); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public String getRightLabel() { |
| 76 | + return rightLabel; |
| 77 | + } |
| 78 | + |
| 79 | + public void setRightLabel(String rightLabel) { |
| 80 | + this.rightLabel = rightLabel; |
| 81 | + if(rightTextView!=null){ |
| 82 | + rightTextView.setText(rightLabel); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments