前言
最近在学自定义View,自己实现一个可垂直显示的TextView,里面会有许多问题,后续会再更新。
提示:以下是本篇文章正文内容,下面案例可供参考
一、View需求
自定义View需要实现的相关功能有:可垂直显示文字,也可水平显示,
二、效果图
自定义Attrs.xml
<!--自定义属性-->
<declare-styleable name="CustomTextView">
<attr name="text" format="string" />
<attr name="textSize" format="dimension" />
<attr name="textColor" format="color" />
<attr name="inputType" format="enum">
<enum name="text" value="1"/>
<enum name="number" value="2"/>
</attr>
<attr name="orientation" format="enum">
<!-- Defines an horizontal widget. -->
<enum name="horizontal" value="0" />
<!-- Defines a vertical widget. -->
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
完整代码
public class CustomTextView extends View {
final String TAG = "-------MyTextView";
private String mText;
private int mTextSize = 15;
private int mTextColor = Color.BLACK;
private int mOrientation;
private final int HORIZONTAL = 0;
private final int VERTICAL = 1;
private int textLength = 0;
//画笔
private Paint mPaint;