基礎(chǔ)屬性
<TextView
android:gravity="right" 設(shè)置文本顯示位置(居右)
android:padding="5dp" 設(shè)置內(nèi)部間距
android:text="忘記密碼寥掐?" 設(shè)置文本
android:textColor="@color/basic_gray1" 設(shè)置字體顏色
android:textSize="12sp" 設(shè)置字體大小
android:textStyle="" 設(shè)置字體風(fēng)格,三個可選值:normal(無效果)百炬,bold(加粗),italic(斜體)
/>
設(shè)置字間距和行間距
字間距:
android:textScaleX:控制字體水平方向的縮放庶弃,默認值1.0f歇攻,值是float
Java中setScaleX(2.0f);
行間距: Android系統(tǒng)中TextView默認顯示中文時會比較緊湊缴守,為了讓每行保持行間距
android:lineSpacingExtra:設(shè)置行間距屡穗,如"3dp"
android:lineSpacingMultiplier:設(shè)置行間距的倍數(shù)村砂,如"1.2"
Java代碼中可以通過: setLineSpacing方法來設(shè)置
自動換行
自動換行通過 android:singleLine 設(shè)置屹逛,默認為 false罕模。
如需要自動換行,可以用:
android:singleLine = "false"
如果要在一行顯示完歇僧,不換行,可以用:
android:singleLine = "true"
除此之外兽埃,可以也設(shè)置多行顯示不完柄错,添加個maxLines的屬性即可
android:maxLines = "3"
省略號和跑馬燈形式
TextView及其子類,當字符內(nèi)容太長顯示不下時可以省略號代替未顯示的字符疫萤;省略號可以在顯示區(qū)域的起始扯饶,中間尾序,結(jié)束位置每币,或者以跑馬燈的方式顯示文字(textview的狀態(tài)為被選中)兰怠。 注意:要和android:maxLines="1"一起用
android:ellipsize="start" 省略號在開頭
android:ellipsize="middle" 省略號在中間
android:ellipsize="end" 省略號在結(jié)尾
android:ellipsize="marquee" 跑馬燈顯示
或者在程序中可通過setEillpsize顯式設(shè)置痕慢。
注: EditText不支持marquee這種模式掖举。
帶圖片(drawableXxx)的TextView
設(shè)置圖片的核心其實就是:drawableXxx;可以設(shè)置四個方向的圖片:
drawableTop(上),drawableButtom(下),drawableLeft(左),drawableRight(右) 另外,你也可以使用drawablePadding來設(shè)置圖片與文字間的間距!
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:drawableTop="@drawable/show1"
android:drawableLeft="@drawable/show1"
android:drawableRight="@drawable/show1"
android:drawableBottom="@drawable/show1"
android:drawablePadding="10dp"
android:text="哈哈" />
代碼設(shè)置圖片
setCompoundDrawables與setCompoundDrawablesWithIntrinsicBounds的區(qū)別
setCompoundDrawablesWithIntrinsicBounds(設(shè)置原圖)
setCompoundDrawables(可以調(diào)整圖片大欣骸)
例1:
public class MainActivity extends Activity {
private TextView txtZQD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtZQD = (TextView) findViewById(R.id.txtZQD);
Drawable[] drawable = txtZQD.getCompoundDrawables();
// 數(shù)組下表0~3,依次是:左上右下
drawable[2].setBounds(0, 0, 40, 40);
txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2],
drawable[3]);
}
}
例2:
mNextButton=(Button)findViewById(R.id.next_button);
Drawable drawable=getResources().getDrawable(R.drawable.im14);
drawable.setBounds(0,0,width,height);//必須設(shè)置,否則無效
mNextButton.setCompoundDrawables(null,null,drawable,null);
//width即為你需要設(shè)置的圖片寬度汁掠,height即為你設(shè)置的圖片的高度
例3:
根據(jù)系統(tǒng)語言設(shè)置圖標位置
//根據(jù)系統(tǒng)語言設(shè)置圖標的顯示位置
String language = Locale.getDefault().getLanguage();
if (language.equals("ar") || language.equals("fa")) {
//如果是從右到左閱讀的語言(阿拉伯等)
Drawable drawable = getResources().getDrawable(R.drawable.edit);
drawable.setBounds(0, 0, drawable.getMinimumWidth()/2, drawable.getMinimumHeight()/2);
tv_sign.setCompoundDrawables(drawable, null, null, null);//設(shè)置textView四個方向上的圖片
tv_sign.setCompoundDrawablePadding(5);//間距
} else {
//從左到右
Drawable drawable = getResources().getDrawable(R.drawable.edit);
drawable.setBounds(0, 0, drawable.getMinimumWidth()/2, drawable.getMinimumHeight()/2);
tv_sign.setCompoundDrawables(null, null, drawable, null);
tv_sign.setCompoundDrawablePadding(5);
}
實現(xiàn)跑馬燈效果的TextView
代碼實現(xiàn):
<TextView
android:id="@+id/txtOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="你整天說著日了狗日了狗,但是你卻沒有來秽之,呵呵呵呵呵呵呵呵呵呵~"/>
各種Span設(shè)置
Android 一個TextView中設(shè)置文字不同字體大小和顏色的最完整方法
將TextView的字體設(shè)置為大小不一
android 一個 textview 設(shè)置不同的字體大小和顏色
步驟如下:
1.定義不同style .
不妨如下定義2個style
<style name="style0">
<item name="android:textSize">19dip</item>
<item name="android:textColor">@color/color1</item>
</style>
<style name="style1">
<item name="android:textSize">23dip</item>
<item name="android:textColor">@color/color2</item>
<item name="android:textStyle">italic</item>
</style>
2 . 通過SpannableString 設(shè)置字符串格式跨细。代碼如下:
mTextView = (TextView)findViewById(R.id.test);
SpannableString styledText = new SpannableString("親愛的小寶扼鞋,你好");
styledText.setSpan(new TextAppearanceSpan(this, R.style.style0), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
styledText.setSpan(new TextAppearanceSpan(this, R.style.style1), 3, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(styledText, TextView.BufferType.SPANNABLE);
使用autoLink屬性識別鏈接類型
為TextView設(shè)置兩種狀態(tài)淫半,程序中可以動態(tài)切換
為TextView設(shè)置兩種狀態(tài)科吭,程序中可以動態(tài)切換
經(jīng)常會需要用文字的兩種狀態(tài)來表示當前系統(tǒng)的某兩種狀態(tài)。比如:
這里的第一個TextView和后兩個TextView就表示了兩種狀態(tài)猴鲫。我們可以在程序的動態(tài)的切換狀態(tài)(而不是直接修改顏色)
可以利用TextView的enable屬性實現(xiàn):
在res中建立一個color文件夾对人,在其中新建一個xml(xxx.xml):
<selector xmlns:android=*"http://schemas.android.com/apk/res/android"* >
<item android:state_enable=*"false" android:color*=*"@color/white"></item>*
<item android:color=*"@color/login_footerbutton_n"></item>*
</selector>
TextView的屬性加一條:
android:textColor=*"@color/xxx"*
TextView的enable的屬性默認為true。
在程序中設(shè)置TextView的狀態(tài):
tabTextView.setEnabled(**false**);
換行后居中顯示
android自定義換行居中CenterTextView
TextView換行居中拂共,每行居中顯示
常見問題
- 設(shè)置了hint屬性后牺弄,對應(yīng)的TextView的最小寬度也會跟你設(shè)置的文字長度有關(guān)
- Android自定義TextView實現(xiàn)文字圖片居中顯示