首先來看下安卓TextView的字體如何設(shè)置成其他格式:
如圖:

首先我們需要一個ttf的字體文件,我從網(wǎng)上下載了個漢儀雪君體繁字體,然后重命名(不能有中文)。
在我們項目的assets文件夾下新建個文件夾fonts將ttf文件復(fù)制粘貼進(jìn)去。

然后就可以設(shè)置我們的字體格式了,代碼很簡單:
AssetManager mgr=getAssets();//得到AssetManagerTypeface tf=Typeface.createFromAsset(mgr, "fonts/hyxjtf.ttf");//根據(jù)路徑得到Typefacetv_set.setTypeface(tf);//設(shè)置字體
3行代碼搞定。
如圖:
可以在布局文件中對TextView添加4行屬性
android:singleLine="true"android:ellipsize="marquee"android:focusable="true"android:focusableInTouchMode="true"
另外TextView的layout_width要比文字的總長度要短一點點,讓singleLine發(fā)揮出效果才行。
這是針對頁面中只有一個TextView的情況下才能這樣搞。
如果文中有多個TextView,你全都要設(shè)置成滾動,那么這樣搞會因為焦點問題導(dǎo)致你設(shè)置的TextView只有一個能達(dá)到效果。
所以這個時候我們就需要重寫TextView了,將isFocused方法返回改為true。
public class MyTextView extends TextView{public MyTextView(Context context) {super(context);// TODO Auto-generated constructor stub}public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); } public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }@Overridepublic boolean isFocused() {// TODO Auto-generated method stubreturn true;}}
然后加到布局中就可以了。
<com.example.view.MyTextView android:id="@+id/tv_scroll1" android:layout_width="150dp" android:layout_height="wrap_content" android:textSize="20sp" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:text="TextView字體滾動效果1" />
<com.example.view.MyTextView android:id="@+id/tv_scroll2" android:layout_width="150dp" android:layout_height="wrap_content" android:textSize="20sp" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:text="TextView字體滾動效果2" />
項目地址:點擊下載源碼
新聞熱點
疑難解答