寫在前面:整理合集的原因很簡單,不管是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