图片类,TextView支持在上、下、左、右四个方向选择放置一张图片。
1.
setCompoundDrawables
(Drawable left, Drawable top, Drawable right, Drawable bottom)
2.
setCompoundDrawablesRelative
(Drawable start, Drawable top, Drawable end, Drawable bottom)
以上这两个方法在传入Drawable对象参数之前,所有Drawable对象都需要先调用任意一个setBounds方法,设定大小。另外,两个方法的区别在于
setCompoundDrawables
固定了图片的上、下、左、右位置;而
setCompoundDrawablesRelative
只固定了上、下两个位置的图片,start、end两个位置,可以根据显示方向的不同,调整左、右分布(
public void setTextDirection(int textDirection) 继承自View类
)。
3.
setCompoundDrawablesRelativeWithIntrinsicBounds
(Drawable start, Drawable top, Drawable end, Drawable bottom)
4.
setCompoundDrawablesRelativeWithIntrinsicBounds
(int start, int top, int end,int bottom)
5.
setCompoundDrawablesWithIntrinsicBounds
(
Drawable
left,
Drawable
top,
Drawable
right,
Drawable
bottom)
6.
setCompoundDrawablesWithIntrinsicBounds
(int left, int top, int right,int bottom)
上面的四个方法“
WithIntrinsicBounds
”表示方法会给
Drawable对象
一个默认的尺寸,如无需控制
Drawable对象
尺寸,不需要对图片做预先处理,int类型的参数表示
Drawable类型
资源ID。
7.
setCompoundDrawablePadding
(int pad) 设置图片和文字之间的间距,四个方向使用同一值。
错误提醒类,TextView可以做页内错误提醒,支持图片和文字。
1.
setError
(CharSequence error)
2.
setError
(CharSequence error, Drawable icon)
3.
setErrorDrawable
(
Drawable
dr
,
TextView
tv)
上面的三个方法都可以让TextView显示一个错误提醒,第三个方法是在4.2更新的版本才会有。Drawable是错误提醒的图片。另外,作为错误提醒的TextView需要设置style="@android:style/Widget.EditText"。效果图如下:
可以看出,错误图标占用了右边的Drawable对象位置。
下面这段代码也可以实现错误提示:
* 页内错误提示,支持文字和图片
final TextView error = new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.LEFT|Gravity.BOTTOM;
error.setLayoutParams(params);
error.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
Drawable icon = getResources().getDrawable(R.drawable.actions_about);
icon.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
error.setError("这是一个显示提醒的TextView", icon);
error.setText("点击弹出错误提示!");
// 在同一个活动界面中只有一个控件可以设置:setFocusableInTouchMode(true),
// 跑马灯和错误提示效果有冲突,所以添加了这个点击事件
error.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
error.setFocusableInTouchMode(true);
viewContainer.addView(error);
位置
和大小类,TextView支持在Java代码中设置位置和大小
1.
setGravity
(int gravity):设置内部文字排列方式;
2.
setHeight
(int pixels):
设置高度参数;
3.
setMaxHeight
(int maxHeight)
4
setMinHeight
(int minHeight)
5.
setWidth
(int pixels):
设置宽度参数;
6.
setMaxWidth
(int maxpixels)
7.
setMinWidth
(int minpixels)
8.
setEms
(int ems):使TextView大小为指定字符宽度.
9.
setMinEms
(int minems) :
使 TextView 的最小宽度为指定个数的字符宽度.
a.
setMaxEms
(int maxems):
使 TextView 的最大宽度为指定个数的字符宽度.
b.
setPadding
(int left,int top, int right, int bottom):设置上、下、左、右四个方向边界空白宽度;
c.
setPaddingRelative
(int start, int top, int end, int bottom) :作用与7同,不同是可以根据排列方向不同把start和end分别赋值到left和right。
文字相关设置:支持提示字符,文字颜色,文字大小,字体,字符类型,行数设置,行间距设置,阴影设置,文字拉伸设置等;
1.
setEllipsize
(
TextUtils.TruncateAt
where)
设置当文字长度超出TextView时的处理。TextUtils.TruncateAt.MARQUEE不处理;TextUtils.TruncateAt.START在开头位置显示省略点;TextUtils.TruncateAt.MIDDLE在中间位置显示省略点;TextUtils.TruncateAt.END在结尾位置显示省略点。
2.
setHighlightColor
(int color) : 提示字符设置-- 背景色
3.
setHint
(int resid) :提示字符资源ID
4.
setHint
(
CharSequence
hint):提示字符串
5.
setHintTextColor
(
ColorStateList
colors): 设置提示字符颜色
6.
setHintTextColor
(int color)
7.
setInputType
(int type): 设置输入类型--android.text.InputType
8.
setRawInputType
(int type)
9.
setSingleLine
(): 设置为单行显示
a.
setSingleLine
(boolean singleLine):设置是否单行显示;
b.
setLines
(int lines):设置TextView高度为lines行字符的高度;
c.
setMaxLines
(int maxlines):设置最大行数
d.
setLineSpacing
(float add,float mult):设置行间距,两数相乘。
e.
setShadowLayer
(float radius,float dx, float dy, int color) :设置文本阴影效果,radius设置模糊效果,值应该大于0,越大越模糊;dx,dy设置作为阴影的文字与原文字在两个坐标轴上的偏移;color设置阴影文字的颜色。
f.
setText
(char[] text,int start, int len) : 设置文字
10.
setText
(
CharSequence
text)
11.
setText
(
CharSequence
text,
TextView.BufferType
type)
TextView.BufferType:EDITABLE、MORMAL、SPANNABLE
12.
setText
(int resid)
13.
setText
(int resid,
TextView.BufferType
type)
14.
setTextAppearance
(
Context
context, int resid)
使用资源设置TextView文字的颜色,字体大小,style,提示文字颜色,文字背景颜色。
15.
setTextColor
(
ColorStateList
colors)
16.
setTextColor
(int color)
17.
setTextIsSelectable
(boolean selectable)
18.
setTextKeepState
(
CharSequence
text)
19.
setTextKeepState
(
CharSequence
text,
TextView.BufferType
type)
1a.
setTextLocale
(
Locale
locale) :
1b.
setTextScaleX
(float size):设置文字拉伸倍数。
1c.
setTextSize
(float size):设置字体大小
1d.
setTextSize
(int unit,float size)
1e.
setTypeface
(
Typeface
tf):设置文字样式为斜体,加黑,等宽...
1f.
setTypeface
(
Typeface
tf, int style)
(未完待续...)
图片类,TextView支持在上、下、左、右四个方向选择放置一张图片。1.setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) 2.setCompoundDrawablesRelative(Drawable start, Drawable top, Drawable end, D
import textr from 'textr' ;
import
locale
from 'textr-
locale
' ;
import ellipses from 'typographic-ellipses' ;
import quotes from 'typographic-quotes' ;
import spaces from 'typographic-single-spaces' ;
const tf = textr ( )
慢慢的开始接触自定义view了,刚开始接触有很多基础是需要整理一下的,比如说Paint的用法,有多少?cavans又有多少方法?如何自定义viewgroup?等等,所以需要一点点开始,今天先记录一下Paint的一些关于文本的用法。
自定义view其实和盖房子一样的道理,首先你需要确定绘制什么样的view,也就是你想盖什么样的房子,是平房还是洋楼,也就是定样,然后你要确定在屏
代码
设置
图片相对位置时,
常用
到如下方法:setCompoundDrawables(left, top, right, bottom)意思:
设置
Drawable显示在text的左、上、右、下位置。方法的源码:/**
* Sets the Drawables (if any) to appear to the left of, above, to the
* right of, and below
setCompoundDrawablesWithIntrinsicBounds 。最近喜欢上了看源码,感觉更能理解其中的逻辑,今天就来看下setCompoundDrawables 和setCompoundDrawablesWithIntrinsicBounds 在源码层次是怎么走的。
用代码给
TextView
设置
drablew时,setCompoundDrawable方法没有起作用,原因是
在android开发过程中,做内容的时候,不仅只有字符,基本都是图文混排,升职个还会对内容中某段文字进行特殊处理,比如:字体加粗、字体变大、改变字体颜色、对某一段文字新增点击事件,等等。这些内容不可能通过一个一个view去拼接,这么复杂的SpannableStringBuilder:可生成字符串生成器,这是内容和标记都可以更改的文本类。常见的富文本修饰如:新增、删除、字体颜色、大小、背景色、图片、上标、下标、删除、点击等,接下来我们将介绍
常用
的一些
public SpannableStringBuilder
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”