css设置字体体时,TextView问题,怎么解决

博客分类:
android:fadingEdge="horizontal" android:scrollHorizontally="true"
name="Theme" parent="android:Theme"
name="Theme.Transparent"
name="android:windowBackground"@drawable/transparent_background
&item name="android:windowIsTranslucent"&true&/item&
name="transparent_background"#
android:name=".Controlls"
android:label="Controlls"
android:theme="@style/Theme.Transparent"
wang_peng1
浏览: 2949442 次
来自: 北京
最后的 -createDialog() 私有方法是怎么回事,没 ...
呵呵,呵呵
感觉你所的不清楚
lstView.setOnTouchLi ...
果然是大神啊!!!
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'一个TextView设置不同大小字体、EditText显示两行不同样式hint
TextView中的字体如何自定义样式(大小颜色等),EditText中的hint如何自定义样式,例如提示消息要显示两行不同大小的文字。
通过SpannableString 类可以轻松到达预期效果。
android:id="@+id/tv" android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="220dp" /&
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="200dp"/&
Activity:
TextView tv = (TextView) findViewById(R.id.tv);
EditText txt = (EditText)findViewById(R.id.txt);
SpannableString ss = new SpannableString("第一排\n第二排");
ss.setSpan(new AbsoluteSizeSpan(20), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new AbsoluteSizeSpan(10), 4, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
txt.setHint(ss);
tv.setText(ss);
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!将TextView的字体设置为大小不一
SpannableStringBuilder text= new SpannableStringBuilder("litter");
text.setSpan(new AbsoluteSizeSpan(60), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableStringBuilder text1= new SpannableStringBuilder("bigger");
text1.setSpan(new AbsoluteSizeSpan(<span style="color:#), 0, text1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView testTextView = (TextView) findViewById(R.id.testTextView);
testTextView.setText(text.append(text1));
备注:各种Span设置
在前面的一个小示例,大家应该也可以看出,要应用一个Span总共分三步:1、构造String
2、构造Span
3、利用SetSpan()对指定范围的String应用这个Span
1、字体颜色设置(ForegroundColorSpan)
SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
spanString.setSpan(span, 1, 5, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
editText.setText(spanString);
效果:2、字体背景颜色(BackgroundColorSpan)
SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);
spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(spanString);
3、字体大小(AbsoluteSizeSpan)
SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
AbsoluteSizeSpan span = new AbsoluteSizeSpan(16);
spanString.setSpan(span, 2, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
editText.setText(spanString);
4、粗体、斜体(StyleSpan)
SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);
spanString.setSpan(span, 1, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(spanString);
5、删除线(StrikethroughSpan)
SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
StrikethroughSpan span = new StrikethroughSpan();
spanString.setSpan(span, 2, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(spanString);
6、下划线(UnderlineSpan)
SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
UnderlineSpan span = new UnderlineSpan();
spanString.setSpan(span, 1, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(spanString);
7、图片置换(ImageSpan)
ImagSpan有很多构造函数,一般是通过传入Drawableg来构造,详细的构造说明看这里:
SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
spanString.setSpan(span, 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(spanString);
这个函数的不同之处在于,前几都是在原来文字的基础上加上特效,而这里却是利用图片将文字替换。如果遇到不支持显示图片的函数,比如前一篇中的canvas绘图。就会退化成String,即以原来的String字符串来显示。
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!android 自定义控件字体,解决字体偏移,卡顿,代码重复等问题
其实,安卓上使用自定义的字体非常得简单,在assets文件夹下面,自己定义一个font文件夹,然后,把自己的字体放进去,可以重命名一下,如图:
这样之后,在代码中,设置一下就可以,如下面所示:
Typeface typeface = Typeface.createFromAsset(_instance.getAssets(), "fonts/mi4.ttf");
textView.setTypeface(typeface);
这样,textView显示的文字就是我们自定义的字体了。
但其实这样还有一些问题,我一个一个说:
字体偏移:
刚开始,我们老大给我们找了一个xx.otf格式的字体,苹果上使用没有问题,安卓上,可以把后缀名改一下,改成ttf,也可以使用,但发生的问题就是,字体是整体偏下的,使用原生的字体,可以正常显示,但使用了这个字体,只能显示上半拉,更关键的是,英文和中文 的偏移量还不一样,弄得我调了半天,最后发现,是字体的问题,必须使用原生的ttf字体,使用软件把otf字体转换成ttf也不行,照样会偏。
解决办法 :
使用原生的ttf字体
卡顿,代码 重复:
如你所知,使用上面的代码 ,就可以给textView设置字体,但这样,没法设置全局字体,每一个控件,都要这样设置,我的应用有很多个界面 ,有很多控件,我每个都这样设置,结果,界面非常卡。还会溢出。
而且,这样,也会有大量的代码重复。
这两个问题,我最后的解决办法如下:
首先:自定义applicaion,现在,一般都是这样自定义的application,然后,在程序如下:
public class MyApplication extends Application {
private static MyApplication _
public void onCreate() {
super.onCreate();
_instance = (MyApplication) getApplicationContext();
typeface = Typeface.createFromAsset(_instance.getAssets(), "fonts/mi4.ttf");
public static
MyApplication getInstace() {
public Typeface getTypeface() {
public void setTypeface(Typeface typeface) {
this.typeface =
如下面的代码所示,我直接声明了一个typeFace,它有自己的getter,setter方法,同时,在程序初使化的时候,我就用下面的代码,把typeface初使化了。
然后,使用自定义的View:
public class MyTextView extends TextView {
public MyTextView(Context context) {
super(context);
setTypeface(MyApplication.getInstace().getTypeface());
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setTypeface(MyApplication.getInstace().getTypeface());
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setTypeface(MyApplication.getInstace().getTypeface());
如图,在自定义的view的构造方法中,直接使用了MyApplication中的typeFace来给view设置字体。
这样,在所有的界面中,我们可以使用我们这个自定义的textView,我们测试并且软件现在已经上线了,完全没有问题。
其它的,如果editText,button等 ,同理。
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!博客分类:
我们通常需要ListView中某一项选中时,他的字体颜色和原来的不一样,这样可以区别。
如何设置字体的颜色呢? 在布局文件中TextColor一项来设置颜色,但是不是只设置一种颜色,而是在不同的条件下设置不同的颜色:
下面是个例子:
&?xml version="1.0" encoding="utf-8" ?&&selector xmlns:android="http://schemas.android.com/apk/res/android"&&item android:state_enabled="false" android:color="@color/orange"&&/item&&item android:state_window_focused="false" android:color="@color/orange"&&/item&&item android:state_pressed="true" android:color="@color/white"&&/item&&item android:state_selected="true" android:color="@color/white"&&/item&&item android:color="@color/orange"&&/item&&/selector&
在获取焦点或者选中的情况下设置为白色,其他情况设置为橘黄色。
gaomingjun
浏览: 56313 次
来自: 西安
刚好用到这块知识
您好! 也给我一份Demo吧,最近在研究push,非常感谢!
您好! 也给我一份Demo吧,最近在研究push,非常感谢!
xiebaolong 写道貌似 https://android ...
貌似 https://android.apis.google. ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 怎么设置大字体 的文章

 

随机推荐