移动开发----BigSmallTextView方便设置大小字体颜色的View
来源:互联网 发布:产品分析软件 编辑:程序博客网 时间:2024/05/16 13:55
项目介绍:
BigSmallTextView
设置大小字体的View
有时候在项目中可能会做下面的效果,为了简便,所以搞了这个View。
效果图:
使用说明:
方法 说明 示例
app:bigText 设置左边的 text app:bigText="你"
setBigText("你");
app:bigTextColor 设置左边 text 的颜色 app:bigTextColor="@color/colorAccent"
setBigTextColor(ContextCompat.getColor(this,R.color.colorAccent));
app:bigTextMarginBottom 如果左右两边字体相差较大,会出现字体下面不对齐的情况,这时候用这个来进行调整(针对左边) app:bigTextMarginBottom="10dp"
setBigTextMarginBottom(10);
app:bigTextSize 设置左边较 text 的大小 app:bigTextSize="20sp"
setBigTextSize(25);
app:smallText 设置右边的 text app:smallText="好"
setSmallText("好");
app:smallTextColor 设置右边 text 的字体颜色 app:smallTextColor="@color/colorPrimary"
setSmallTextColor(ContextCompat.getColor(this, R.color.colorPrimary));
app:smallTextMarginBottom 如果左右两边字体相差较大,会出现字体下面不对齐的情况,这时候用这个来进行调整(针对右边) app:smallTextMarginBottom="10dp"
setSmallTextMarginBottom(10);
app:smallTextSize 右边 text 字体的大小 app:smallTextSize="16sp"
setSmallTextMarginBottom(10);
app:textOffset 设置左边和右边 text 之间的距离 app:textOffset="10dp"
setTextOffset(10);
也可以在代码中使用 setXXX() 方法来设置。 例子:
mBigSmallTextView
.setBigTextSize(25) .setSmallTextSize(16) .setBigText("你") .setSmallText("好");
代码中设置字体大小单位默认为 sp ,设置距离单位默认为 dp 。
原则上控件的样式是左边的 text 大,右边的 text 小,但如果你愿意,也可以设置成左边的 text 小,右边的 text 大。
下面是代码类:
package com.example.bigsmalltext;import android.content.Context;import android.content.res.Resources;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.text.TextPaint;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.util.TypedValue;import android.view.View;/** * Created by lzx on 2017/7/13. */public class BigSmallTextView extends View { private float bigTextSize; private float smallTextSize; private String bigText = ""; private String smallText = ""; private int bigTextColor; private int smallTextColor; private Paint mBigPaint, mSmallPain; private Rect mBigRect, mSmallRect; private float bigText_marginBottom; private float smallText_marginBottom; private float textOffset = 0; //两个文字之间的距离 private DisplayMetrics metrics; public BigSmallTextView(Context context) { this(context, null); } public BigSmallTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public BigSmallTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); Context c = getContext(); Resources r; if (c == null) r = Resources.getSystem(); else r = c.getResources(); metrics = r.getDisplayMetrics(); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BigSmallTextView); bigTextSize = typedArray.getDimension(R.styleable.BigSmallTextView_bigTextSize, 16); smallTextSize = typedArray.getDimension(R.styleable.BigSmallTextView_smallTextSize, 14); bigText = typedArray.getString(R.styleable.BigSmallTextView_bigText); smallText = typedArray.getString(R.styleable.BigSmallTextView_smallText); bigTextColor = typedArray.getColor(R.styleable.BigSmallTextView_bigTextColor, Color.BLACK); smallTextColor = typedArray.getColor(R.styleable.BigSmallTextView_smallTextColor, Color.BLACK); bigText_marginBottom = typedArray.getDimension(R.styleable.BigSmallTextView_bigTextMarginBottom, 0); smallText_marginBottom = typedArray.getDimension(R.styleable.BigSmallTextView_smallTextMarginBottom, 0); textOffset = typedArray.getDimension(R.styleable.BigSmallTextView_textOffset, 0); typedArray.recycle(); mBigPaint = new TextPaint(); mBigPaint.setAntiAlias(true); mSmallPain = new TextPaint(); mSmallPain.setAntiAlias(true); mBigRect = new Rect(); mSmallRect = new Rect(); } public BigSmallTextView setSmallText(String smallText) { this.smallText = smallText; return this; } public BigSmallTextView setBigText(String bigText) { this.bigText = bigText; return this; } public BigSmallTextView setTextOffset(int textOffset) { this.textOffset = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, textOffset, metrics); return this; } public BigSmallTextView setSmallTextMarginBottom(float smallText_marginBottom) { this.smallText_marginBottom = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, smallText_marginBottom, metrics); return this; } public BigSmallTextView setBigTextMarginBottom(float bigText_marginBottom) { this.bigText_marginBottom = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, bigText_marginBottom, metrics); return this; } public BigSmallTextView setBigTextColor(int bigTextColor) { this.bigTextColor = bigTextColor; return this; } public BigSmallTextView setSmallTextColor(int smallTextColor) { this.smallTextColor = smallTextColor; return this; } public BigSmallTextView setSmallTextSize(float smallTextSize) { this.smallTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, smallTextSize, metrics); return this; } public BigSmallTextView setBigTextSize(float bigTextSize) { this.bigTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, bigTextSize, metrics); return this; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); mBigPaint.setColor(bigTextColor); mBigPaint.setTextSize(bigTextSize); mSmallPain.setColor(smallTextColor); mSmallPain.setTextSize(smallTextSize); mBigPaint.getTextBounds(bigText, 0, bigText.length(), mBigRect); mSmallPain.getTextBounds(smallText, 0, smallText.length(), mSmallRect); int bigTextWidth = (int) mBigPaint.measureText(bigText); int smallTextWidth = (int) mSmallPain.measureText(smallText); //控件的实际宽高 int realWidth = (int) (bigTextWidth + smallTextWidth + textOffset); int realHeight; if (mBigRect.height() > mSmallRect.height()) { realHeight = (int) (mBigRect.height() + mBigPaint.descent() + bigText_marginBottom + smallText_marginBottom); } else { realHeight = (int) (mSmallRect.height() + mSmallPain.descent() + bigText_marginBottom + smallText_marginBottom); } int width = 0; int height = 0; if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) { width = realWidth; height = realHeight; } else if (widthMode == MeasureSpec.AT_MOST) { width = realWidth; height = heightSize; } else if (heightMode == MeasureSpec.AT_MOST) { width = widthSize; height = realHeight; } setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int paddingTop = getPaddingTop(); int paddingBottom = getPaddingBottom(); int height = getHeight() - paddingTop - paddingBottom; //画文字 float bigTextX = 0; float bigTextY = height - mBigPaint.descent() - bigText_marginBottom; canvas.drawText(bigText, bigTextX, bigTextY, mBigPaint); float smallTextX = mBigPaint.measureText(bigText) + textOffset; float smallTextY = height - mSmallPain.descent() - smallText_marginBottom; canvas.drawText(smallText, smallTextX, smallTextY, mSmallPain); }}接下来是values下的文件attrs:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="BigSmallTextView"> <attr name="bigTextSize" format="dimension"/> <attr name="smallTextSize" format="dimension"/> <attr name="bigText" format="string"/> <attr name="smallText" format="string"/> <attr name="bigTextColor" format="color"/> <attr name="smallTextColor" format="color"/> <attr name="bigTextMarginBottom" format="dimension"/> <attr name="smallTextMarginBottom" format="dimension"/> <attr name="textOffset" format="dimension"/> </declare-styleable></resources>布局文件:
<com.example.bigsmalltext.BigSmallTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" app:bigText="520" app:bigTextColor="@color/colorPrimary" app:bigTextSize="40sp" app:smallText="哈喽,2017/7/14" app:smallTextColor="@color/colorAccent" app:smallTextSize="20sp"/>
- 移动开发----BigSmallTextView方便设置大小字体颜色的View
- 换行的字体的设置大小颜色
- webview设置字体颜色、大小
- iOS开发_textField的placeholder属性的字体颜色、大小的设置
- iOS开发设置textField的属性placeholder的字体颜色及大小
- ios设置textField的placeholder的字体颜色、大小
- TextView设置不同的字体颜色大小的方法
- 动态设置编辑框的大小、 文本字体、颜色等
- Java中怎么设置JLabel的字体样式,大小,颜色?
- MFC Static文字的字体、大小、颜色设置
- android 通过一Textview设置不同大小的字体颜色
- <p>标题</p>能设置字体的大小和颜色
- 设置UINavigationController的navigationBar标题,实现自定义字体、大小、颜色。
- actionBar的tab字体颜色大小样式设置
- 设置字体的行距和大小颜色等
- segmentcontroler设置被选中状态下字体的颜色 大小
- WebView设置文本内容字体的大小以及颜色
- 设置TabBar的字体颜色大小注意细节
- 护眼色DIY调节
- 第三周 leetcode 102. Binary Tree Level Order Traversal(Medium)
- 数组方式赋值字符串及字面值常量赋值字符串的区别
- ros源码分析(1)—roscore概况
- ionic之 bug
- 移动开发----BigSmallTextView方便设置大小字体颜色的View
- NullPointerException: Attempt to invoke virtual method 'int com.huawei.lcagent.client.LogCollectMana
- libevent
- springloaded--热部署设置,替代jrebel
- 使用mysql事务报错:Lock wait timeout exceeded; try restarting transaction Query:
- 医疗化验单的进展和展望
- 【集训】jzoj 2017.7.14 noip模拟赛A 总结
- C++编程规范19:总是初始化变量
- Jmeter JDBC显示事务处理(优化版本)