原文連接:http://www.jianshu.com/p/4e7d7a08fc7ejava
這篇文章記錄一下TextView中不經常使用的幾個方法,直接上動圖:android
#####setTextIsSelectable(boolean selectable):ide
setTextIsSelectable(boolean selectable)對應xml中的android:textIsSelectable,用於聲明TextView中的內容是否可被選中。測試
setTextIsSelectable截圖: this
#####setSelectAllOnFocus(boolean selectAllOnFocus):.net
setSelectAllOnFocus(boolean selectAllOnFocus)對應xml中的android:selectAllOnFocus,用於聲明TextView/EditText實例在獲取焦點後是否選中所有內容。code
setSelectAllOnFocus截圖: orm
#####setHighlightColor(@ColorInt int color):xml
setHighlightColor(@ColorInt int color)對應xml中的android:textColorHighlight,用於聲明TextView中被選中內容的高亮背景色。圖片
setHighlightColor截圖:
#####連接相關: setAutoLinkMask(int mask)
setAutoLinkMask(int mask)對應xml中的android:autoLink,用於聲明TextView實例可識別的連接類型。
setLinkTextColor(@ColorInt int color)
setLinkTextColor(@ColorInt int color)對應xml中的android:textColorLink,用於設置TextView實例中連接的顏色。
setLinksClickable(boolean whether)
setLinksClickable(boolean whether)對應xml中的android:linksClickable,用於設置TextView實例中的連接是否可點擊/點擊是否執行對應動做。
#####setTextScaleX(float size)
setTextScaleX(float size)對應xml中的android:textScaleX,用於設置TextView實例中文字橫向拉伸倍數。
setTextScaleX截圖(黃色高亮區域):
#####設置TextView高度爲行數相關: setMinLines(int minlines)
setMinLines(int minlines)對應xml中的android:minLines,用於設置TextView最小高度爲指定行高度。
setMaxLines(int maxlines)
setMaxLines(int maxlines)對應xml中的android:maxLines,用於設置TextView最大高度爲指定行高度。
setLines(int lines)
setLines(int lines)對應xml中的android:lines,用於設置TextView精確高度爲指定行高度。
設置TextView高度爲行數相關截圖(黃色高亮區域):
#####android:password
android:password 用於設置TextView中內容是否爲明文。
#####setShadowLayer(float radius, float dx, float dy, int color)
setShadowLayer(float radius, float dx, float dy, int color)對應xml中的android:shadowRadius,android:shadowDx,android:shadowDy,android:shadowColor,用於設置文字陰影。
setShadowLayer截圖(黃色高亮區域):
#####setEllipsize(TextUtils.TruncateAt where)
setEllipsize(TextUtils.TruncateAt where)對應xml中的android:ellipsize,用於文字省略樣式設置。
#####setCompoundDrawablePadding(int pad)
setCompoundDrawablePadding(int pad)對應xml中的android:drawablePadding,用於設置TextView中文字與四周drawable的間距。
setCompoundDrawablePadding截圖(黃色高亮區域):
#####setCompoundDrawableTintList(@Nullable ColorStateList tint)
setCompoundDrawableTintList(@Nullable ColorStateList tint)對應xml中的android:drawableTint,用於設置TextView四周drawable的着色。
**設置TextView的四周圖片的顏色,只有在SDK>=23狀況下有效! 低版本兼容方法: {@link Drawable#setColorFilter(int, PorterDuff.Mode)}方法,在java代碼中手動爲四周圖片進行着色 **
示例: 經試驗在Android 4.22上,如下方法無效: DrawableCompat.setTint(d1,Color.RED); DrawableCompat.setTintMode(d1, PorterDuff.Mode.SRC_ATOP); 正確作法: Drawable d1 = getResources().getDrawable(R.mipmap.i1).mutate(); d1.setBounds(0,0,64,64); d1.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); Drawable d2 = getResources().getDrawable(R.mipmap.i2).mutate(); d2.setBounds(0,0,64,64); d2.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); Drawable[] drawables = tv_drawableTint.getCompoundDrawables(); drawables[0] = d1; drawables[2] = d2; tv_drawableTint.setCompoundDrawables(drawables[0],drawables[1],drawables[2],drawables[3]);
#####setLineSpacing(float add, float mult):
setLineSpacing(float add, float mult)對應xml中的android:lineSpacingMultiplier和android:lineSpacingExtra,用於設置多行TextView中單行高度。
setLineSpacing截圖(黃色高亮區域):
#####setLetterSpacing(float letterSpacing)
setLetterSpacing(float letterSpacing)對應xml中的android:letterSpacing,用於設置文本的字符間距。
setLetterSpacing截圖(黃色高亮區域):
#####setAllCaps(boolean allCaps)
setAllCaps(boolean allCaps)對應xml中的android:textAllCaps,用於設置TextView中的字符是否所有顯示爲大寫形式。
#####setTransformationMethod(TransformationMethod method)
setTransformationMethod(TransformationMethod method)用於設置TextView展現內容與實際內容之間的轉換規則。
#####setTextSize(int unit, float size)
setTextSize(int unit, float size)用於設置TextView實例的文字尺寸爲指定單位unit的指定值size。
xml及Java代碼:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_article1" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="textIsSelectable:文字是否可被選中" /> <TextView android:id="@+id/tv_textIsSelectable" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="測試文字是否可被選中\n測試文字是否可被選中" android:textIsSelectable="true" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="selectAllOnFocus:文字獲取焦點後是否被全選" /> <EditText android:id="@+id/tv_selectAllOnFocus" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="文字獲取焦點後是否被全選" android:textIsSelectable="true" android:selectAllOnFocus="true" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="textColorHighlight:文字被選中時的高亮背景色" /> <TextView android:id="@+id/tv_textColorHighlight" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="文字被選中時的高亮背景色\n文字被選中時的高亮背景色" android:textIsSelectable="true" android:textColorHighlight="#36B34A" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="連接相關:autoLink+textColorLink+linksClickable" /> <TextView android:id="@+id/tv_link" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="http://baidu.com\n爲啥耳機福克斯\n2346726\nhsfkd@55.com" android:autoLink="all" android:textColorLink="@color/colorPrimary" android:linksClickable="true" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="textScaleX:文字橫向拉伸倍數" /> <TextView android:id="@+id/tv_textScaleX" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="hgjkshgkhsdkfgjldfjhgkhshjg;ld" android:textScaleX="2.6" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="minLines:設置TextView最小高度爲指定行高度\n當前:minLines=3" /> <TextView android:id="@+id/minLines" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="文字只有一行" android:minLines="3" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="maxLines:設置TextView最大高度爲指定行高度\n當前:maxLines=2 文字有5行" /> <TextView android:id="@+id/maxLines" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="第一行文字\n第二行文字\n第三行文字\n第四行文字\n第五行文字" android:maxLines="2" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="lines:設置TextView精確高度爲指定行高度\n當前:lines=2 文字有5行" /> <TextView android:id="@+id/lines" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="第一行文字\n第二行文字\n第三行文字\n第四行文字\n第五行文字" android:lines="2" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="password:設置內容是否爲明文" /> <TextView android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="測試文字測試文字測試文字測試文字測試文字" android:password="true" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="shadow:陰影相關" /> <TextView android:id="@+id/tv_shadow" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="測試文字測試文字測試文字測試文字測試文字" android:shadowDx="12.0" android:shadowDy="16.0" android:shadowColor="#EA2000" android:shadowRadius="8.0" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="ellipsize:文字省略樣式設置" /> <TextView android:id="@+id/tv_ellipsize" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字測試文字" android:maxLines="1" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="drawablePadding:設置文字與TextView四周drawable的間距" /> <TextView android:id="@+id/tv_drawablePadding" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="測試文字測試文字" android:drawableLeft="@mipmap/ic_launcher" android:drawablePadding="32dp" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="drawableTint:設置TextView四周drawable的着色" /> <TextView android:id="@+id/tv_drawableTint" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="測試文字測試文字" android:gravity="center" android:drawableLeft="@mipmap/ic_launcher" android:drawableTop="@mipmap/ic_launcher" android:drawableRight="@mipmap/ic_launcher" android:drawableBottom="@mipmap/ic_launcher" android:drawableTint="#3A92FF" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="lineSpacing:設置多行TextView中單行高度" /> <TextView android:id="@+id/tv_lineSpacing_original" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/black" android:text="測試文字測試文字測試文字測試文字\n測試文字測試文字測試文字測試文字\n測試文字測試文字測試文字測試文字" android:lineSpacingMultiplier="1.0" android:lineSpacingExtra="0dp" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@android:color/darker_gray" /> <TextView android:id="@+id/tv_lineSpacing_setting" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/black" android:text="測試文字測試文字測試文字測試文字\n測試文字測試文字測試文字測試文字\n測試文字測試文字測試文字測試文字" android:lineSpacingMultiplier="2.0" android:lineSpacingExtra="16dp" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="letterSpacing:設置文本的字符間距" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="letterSpacing=2.0" android:letterSpacing="2.0" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="letterSpacing=-0.2" android:letterSpacing="-0.2" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="textAllCaps:文字是否所有大寫形式" /> <TextView android:id="@+id/tv_textAllCaps" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="ghkldjgkldjlkldffjgjfdslkfh;hkslkhfgkdj" android:textAllCaps="true" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="setTransformationMethod:設置TextView展現內容與實際內容之間的轉換規則" /> <TextView android:id="@+id/tv_SetTransformationMethod" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="abcdefghijklmn" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="setTextSize(int unit, float size):設置文字尺寸爲指定單位unit的指定值size" /> <TextView android:id="@+id/tv_setTextSize" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:textColor="@android:color/black" android:text="setTextSize(int unit, float size)" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /> </LinearLayout> </ScrollView>
public class Article1Activity extends AppCompatActivity { private TextView tv_ellipsize = null; int index = 0; private TextUtils.TruncateAt[] ellipsizes = new TextUtils.TruncateAt[]{ TextUtils.TruncateAt.START, TextUtils.TruncateAt.MIDDLE, TextUtils.TruncateAt.END }; Handler handlerEllipsize = new Handler(){ @Override public void handleMessage(Message msg) { index = ++index%ellipsizes.length; tv_ellipsize.setEllipsize(ellipsizes[index]); handlerEllipsize.sendMessageDelayed(handlerEllipsize.obtainMessage(),2000); } }; Handler handlerTransformationMethod = new Handler(){ @Override public void handleMessage(Message msg) { int what = 0; if(msg.what == 0){ //大寫 tv_SetTransformationMethod.setTransformationMethod(new MyTransformationMethod(true)); what = 1; }else { //小寫 tv_SetTransformationMethod.setTransformationMethod(new MyTransformationMethod()); what = 0; } handlerTransformationMethod.sendEmptyMessageDelayed(what,2000); } }; private TextView tv_SetTransformationMethod = null; private int[] units = new int[]{ TypedValue.COMPLEX_UNIT_PX, TypedValue.COMPLEX_UNIT_DIP, TypedValue.COMPLEX_UNIT_SP }; Handler handlerSetTextSize = new Handler(){ @Override public void handleMessage(Message msg) { index = ++index%units.length; tv_setTextSize.setTextSize(units[index],24.0f); handlerSetTextSize.sendMessageDelayed(handlerSetTextSize.obtainMessage(),2000); } }; private TextView tv_setTextSize = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_article1); tv_ellipsize = (TextView) findViewById(R.id.tv_ellipsize); handlerEllipsize.sendMessage(handlerEllipsize.obtainMessage()); tv_SetTransformationMethod = (TextView) findViewById(R.id.tv_SetTransformationMethod); handlerTransformationMethod.sendEmptyMessage(0); tv_setTextSize = (TextView) findViewById(R.id.tv_setTextSize); handlerSetTextSize.sendEmptyMessage(0); } class MyTransformationMethod implements TransformationMethod { private boolean upperCase = false; public MyTransformationMethod(boolean ... upperCase){ this.upperCase = (upperCase==null||upperCase.length<=0)?false:upperCase[0]; } @Override public CharSequence getTransformation(CharSequence source, View view) { if(this.upperCase){ return TextUtils.isEmpty(source)?"":source.toString().toUpperCase(getResources().getConfiguration().locale)+"大寫"; }else{ return TextUtils.isEmpty(source)?"":source.toString().toLowerCase(getResources().getConfiguration().locale)+"小寫"; } } @Override public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect) { } } }
以上就是我的實驗和分析的一點結果,如有錯誤,請各位同窗留言告知!
That's all !