首先来看下安卓TextView的字体如何设置成其他格式:
如图:
首先我们需要一个ttf的字体文件,我从网上下载了个汉仪雪君体繁字体,然后重命名(不能有中文)。
在我们项目的assets文件夹下新建个文件夹fonts将ttf文件复制粘贴进去。
然后就可以设置我们的字体格式了,代码很简单:
AssetManager mgr=getAssets();//得到AssetManagerTypeface tf=Typeface.createFromAsset(mgr, "fonts/hyxjtf.ttf");//根据路径得到Typefacetv_set.setTypeface(tf);//设置字体
3行代码搞定。
如图:
可以在布局文件中对TextView添加4行属性
android:singleLine="true"android:ellipsize="marquee"android:focusable="true"android:focusableInTouchMode="true"
另外TextView的layout_width要比文字的总长度要短一点点,让singleLine发挥出效果才行。
这是针对页面中只有一个TextView的情况下才能这样搞。
如果文中有多个TextView,你全都要设置成滚动,那么这样搞会因为焦点问题导致你设置的TextView只有一个能达到效果。
所以这个时候我们就需要重写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" />
项目地址:点击下载源码
新闻热点
疑难解答