Android基本控件使用合集【1】之TextView

写在前面:整理合集的原因很简单,不管是android开发的老手还是新手,不可能把控件所有的属性都记下来。老手可能看看代码提示,找一找就找到了,因为他们以前都用过。而对于新手来说,有可能都不知道有这个属性,极其容易走弯路。我就决定,整理一套合集,供新手git新技能,老手巩固基础知识,自己在其中也得到了成长,其乐融融。

TextView的使用(未完善,持续更新)

一:属性

1.1 TextView基本属性、常用属性讲解。

像layout_width、margin、padding这样的就不介绍了,都是View的方法。

android:text=""                      //显示的文本。
android:textColor="#000000"          //显示文本的颜色。
android:textSize="16sp"              //字体大小,注:字体大小的单位是sp。
android:hint="灰度显示"               //提示文字,当text属性为空时显示。
android:textColorHint="#ff0000"      //提示的颜色。

1.2 TextView固定行数显示、超出行数显示小数点。

android:maxLines="2"         //文本显示的最大行数,参数为整数
android:ellipsize="end"      //文本超出部分的显示效果

关于ellipsize属性的详细讲解:

android:ellipsize="start"—– 文本超出部分显示省略号,省略号显示在开头 "...roid"
android:ellipsize="end" —— 省略号显示在结尾 "andr..."
android:ellipsize="middle" —— 省略号显示在中间 "an...id"
android:ellipsize="marquee" —— 以横向滚动方式显示文本(需要当前控件获得焦点)
android:ellipsize="none" —— 不显示(这是默认属性)

1.3 TextView显示富文本,比如字体变色等。

textView.setText(Html.fromHtml("常用网站:<font color='red'>百度</font>、<font color='red'>好123</font>..."), TextView.BufferType.SPANNABLE);

二:方法

2.1 常用操作方法

textView.setText("我是设置的内容");    //设置text显示的文本(还有其他参数在下文详细介绍)

textView.append("我是追加的内容");    //在text显示的基础上追加显示内容


以下是属于TextView自己的属性大全

操作方法:按住Ctrl + 鼠标左键(mac是command+鼠标左键)点进进入TextView的源码,在开头的注释中,就有TextView的所有属性,(特别说一下,像 )以下是详细代码:

 * @attr ref android.R.styleable#TextView_text
 * @attr ref android.R.styleable#TextView_bufferType
 * @attr ref android.R.styleable#TextView_hint
 * @attr ref android.R.styleable#TextView_textColor
 * @attr ref android.R.styleable#TextView_textColorHighlight
 * @attr ref android.R.styleable#TextView_textColorHint
 * @attr ref android.R.styleable#TextView_textAppearance
 * @attr ref android.R.styleable#TextView_textColorLink
 * @attr ref android.R.styleable#TextView_textSize
 * @attr ref android.R.styleable#TextView_textScaleX
 * @attr ref android.R.styleable#TextView_fontFamily
 * @attr ref android.R.styleable#TextView_typeface
 * @attr ref android.R.styleable#TextView_textStyle
 * @attr ref android.R.styleable#TextView_cursorVisible
 * @attr ref android.R.styleable#TextView_maxLines
 * @attr ref android.R.styleable#TextView_maxHeight
 * @attr ref android.R.styleable#TextView_lines
 * @attr ref android.R.styleable#TextView_height
 * @attr ref android.R.styleable#TextView_minLines
 * @attr ref android.R.styleable#TextView_minHeight
 * @attr ref android.R.styleable#TextView_maxEms
 * @attr ref android.R.styleable#TextView_maxWidth
 * @attr ref android.R.styleable#TextView_ems
 * @attr ref android.R.styleable#TextView_width
 * @attr ref android.R.styleable#TextView_minEms
 * @attr ref android.R.styleable#TextView_minWidth
 * @attr ref android.R.styleable#TextView_gravity
 * @attr ref android.R.styleable#TextView_scrollHorizontally
 * @attr ref android.R.styleable#TextView_password
 * @attr ref android.R.styleable#TextView_singleLine
 * @attr ref android.R.styleable#TextView_selectAllOnFocus
 * @attr ref android.R.styleable#TextView_includeFontPadding
 * @attr ref android.R.styleable#TextView_maxLength
 * @attr ref android.R.styleable#TextView_shadowColor
 * @attr ref android.R.styleable#TextView_shadowDx
 * @attr ref android.R.styleable#TextView_shadowDy
 * @attr ref android.R.styleable#TextView_shadowRadius
 * @attr ref android.R.styleable#TextView_autoLink
 * @attr ref android.R.styleable#TextView_linksClickable
 * @attr ref android.R.styleable#TextView_numeric
 * @attr ref android.R.styleable#TextView_digits
 * @attr ref android.R.styleable#TextView_phoneNumber
 * @attr ref android.R.styleable#TextView_inputMethod
 * @attr ref android.R.styleable#TextView_capitalize
 * @attr ref android.R.styleable#TextView_autoText
 * @attr ref android.R.styleable#TextView_editable
 * @attr ref android.R.styleable#TextView_freezesText
 * @attr ref android.R.styleable#TextView_ellipsize
 * @attr ref android.R.styleable#TextView_drawableTop
 * @attr ref android.R.styleable#TextView_drawableBottom
 * @attr ref android.R.styleable#TextView_drawableRight
 * @attr ref android.R.styleable#TextView_drawableLeft
 * @attr ref android.R.styleable#TextView_drawableStart
 * @attr ref android.R.styleable#TextView_drawableEnd
 * @attr ref android.R.styleable#TextView_drawablePadding
 * @attr ref android.R.styleable#TextView_drawableTint
 * @attr ref android.R.styleable#TextView_drawableTintMode
 * @attr ref android.R.styleable#TextView_lineSpacingExtra
 * @attr ref android.R.styleable#TextView_lineSpacingMultiplier
 * @attr ref android.R.styleable#TextView_marqueeRepeatLimit
 * @attr ref android.R.styleable#TextView_inputType
 * @attr ref android.R.styleable#TextView_imeOptions
 * @attr ref android.R.styleable#TextView_privateImeOptions
 * @attr ref android.R.styleable#TextView_imeActionLabel
 * @attr ref android.R.styleable#TextView_imeActionId
 * @attr ref android.R.styleable#TextView_editorExtras
 * @attr ref android.R.styleable#TextView_elegantTextHeight
 * @attr ref android.R.styleable#TextView_letterSpacing
 * @attr ref android.R.styleable#TextView_fontFeatureSettings
 * @attr ref android.R.styleable#TextView_breakStrategy
 * @attr ref android.R.styleable#TextView_hyphenationFrequency
 * @attr ref android.R.styleable#TextView_autoSizeTextType
 * @attr ref android.R.styleable#TextView_autoSizeMinTextSize
 * @attr ref android.R.styleable#TextView_autoSizeMaxTextSize
 * @attr ref android.R.styleable#TextView_autoSizeStepGranularity
 * @attr ref android.R.styleable#TextView_autoSizePresetSizes

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章