├── README.md ├── attrs.xml └── LabelView.java /README.md: -------------------------------------------------------------------------------- 1 | #LabelView 2 | 3 | ##一个能设置上下左右文本的textview 并支持自定义属性 4 | 5 | 布局中设置 6 | ![Alt text](http://7xu3h5.com1.z0.glb.clouddn.com/QQ%E6%88%AA%E5%9B%BE20160628152103.png) 7 | 8 | 代码中设置 9 | 10 | 11 | ![Alt text](http://7xu3h5.com1.z0.glb.clouddn.com/QQ%E6%88%AA%E5%9B%BE20160628151726.png) 12 | 13 | 支持的属性 14 | 15 | ![Alt text](http://7xu3h5.com1.z0.glb.clouddn.com/QQ%E6%88%AA%E5%9B%BE20160628151835.png) 16 | 17 | 效果 18 | 19 | ![Alt text](http://7xu3h5.com1.z0.glb.clouddn.com/QQ%E6%88%AA%E5%9B%BE20160628152132.png) 20 | -------------------------------------------------------------------------------- /attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /LabelView.java: -------------------------------------------------------------------------------- 1 | package com.softinfo.zdl.smartlife.view; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.text.SpannableString; 6 | import android.text.SpannableStringBuilder; 7 | import android.text.TextUtils; 8 | import android.text.style.TextAppearanceSpan; 9 | import android.util.AttributeSet; 10 | import android.view.Gravity; 11 | import android.widget.TextView; 12 | 13 | import come.softinfo.zdl.smartlife.R; 14 | 15 | 16 | public class LabelView extends TextView { 17 | 18 | public CharSequence mLeftText, mTopText, mRightText, mBottomText; 19 | private int mLeftTextAppearance, mTopTextAppearance, mRightTextAppearance, 20 | mBottomTextAppearance; 21 | private CharSequence mText; 22 | 23 | 24 | public LabelView(Context context) { 25 | this(context, null); 26 | } 27 | 28 | 29 | public LabelView(Context context, AttributeSet attrs) { 30 | this(context, attrs, 0); 31 | } 32 | 33 | 34 | public LabelView(Context context, AttributeSet attrs, int defStyleAttr) { 35 | super(context, attrs, defStyleAttr); 36 | TypedArray a = context.obtainStyledAttributes(attrs, 37 | R.styleable.LabelView); 38 | mLeftText = a.getText(R.styleable.LabelView_leftText); 39 | mTopText = a.getText(R.styleable.LabelView_topText); 40 | mRightText = a.getText(R.styleable.LabelView_rightText); 41 | mBottomText = a.getText(R.styleable.LabelView_bottomText); 42 | mLeftTextAppearance = a.getResourceId( 43 | R.styleable.LabelView_leftTextAppearance, 0); 44 | mTopTextAppearance = a.getResourceId( 45 | R.styleable.LabelView_topTextAppearance, 0); 46 | mRightTextAppearance = a.getResourceId( 47 | R.styleable.LabelView_rightTextAppearance, 0); 48 | mBottomTextAppearance = a.getResourceId( 49 | R.styleable.LabelView_bottomTextAppearance, 0); 50 | int gravity = a.getInt(R.styleable.LabelView_android_gravity, 51 | Gravity.CENTER); 52 | a.recycle(); 53 | setGravity(gravity); 54 | setText(super.getText()); 55 | } 56 | 57 | @Override 58 | public void setText(CharSequence mainText, BufferType type) { 59 | super.setText(mainText, type); 60 | mText = mainText; 61 | CharSequence text = mainText; 62 | if (!TextUtils.isEmpty(mLeftText)) { 63 | text = buildTextLeft(mLeftText.toString(), text, 64 | mLeftTextAppearance); 65 | } 66 | if (!TextUtils.isEmpty(mRightText)) { 67 | text = buildTextRight(text, mRightText.toString(), 68 | mRightTextAppearance); 69 | } 70 | if (!TextUtils.isEmpty(mTopText)) { 71 | text = new SpannableStringBuilder("\n").append(text); 72 | text = buildTextLeft(mTopText.toString(), text, mTopTextAppearance); 73 | } 74 | if (!TextUtils.isEmpty(mBottomText)) { 75 | text = new SpannableStringBuilder(text).append("\n"); 76 | text = buildTextRight(text, mBottomText.toString(), 77 | mBottomTextAppearance); 78 | } 79 | if (!TextUtils.isEmpty(text)) { 80 | super.setText(text, type); 81 | if (!TextUtils.isEmpty(mLeftText)) { 82 | mLeftText = ""; 83 | } 84 | if (!TextUtils.isEmpty(mRightText)) { 85 | mRightText = ""; 86 | } 87 | if (!TextUtils.isEmpty(mTopText)) { 88 | mTopText = ""; 89 | } 90 | if (!TextUtils.isEmpty(mBottomText)) { 91 | mBottomText = ""; 92 | } 93 | } 94 | } 95 | 96 | 97 | private CharSequence buildTextLeft(CharSequence head, CharSequence foot, int style) { 98 | SpannableString leftText = format(getContext(), head, style); 99 | SpannableStringBuilder builder = new SpannableStringBuilder( 100 | leftText).append(foot); 101 | return builder.subSequence(0, builder.length()); 102 | } 103 | 104 | 105 | //todo 106 | private CharSequence buildTextRight(CharSequence head, CharSequence foot, int style) { 107 | SpannableString rightText = format(getContext(), foot, style); 108 | SpannableStringBuilder builder = new SpannableStringBuilder( 109 | head).append(rightText); 110 | return builder.subSequence(0, builder.length()); 111 | } 112 | 113 | 114 | public SpannableString format(Context context, CharSequence text, int style) { 115 | SpannableString spannableString = new SpannableString(text); 116 | spannableString.setSpan(new TextAppearanceSpan(context, style), 0, 117 | text.length(), 0); 118 | return spannableString; 119 | } 120 | 121 | 122 | @Override 123 | public CharSequence getText() { 124 | return mText; 125 | } 126 | } 127 | --------------------------------------------------------------------------------