TextView常用设置操作方法

图片类,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(Drawabledr,TextViewtv)

上面的三个方法都可以让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) 

4setMinHeight(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)

(未完待续...)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值