textView相关知识

news/2024/7/8 11:25:49

一、给textview设置颜色为#fffff

有时候我们会从服务器获得数据,但是后台给的服务器的值是比如:#B373FB

此时要想给TextView设置文字直接设置textview.setTextColor();是不行的。此时我们可以这样

String str = floorcolor;
int id = Color.parseColor(str);//#ffffff设置成color对象
tv_select1.setTextColor(id);

tv_select1为TextView,floorcolor比如:"#ffffff"


二、TextView的跑马灯效果,这个很简单

为了解决TextView不会在失去焦点的时候不滚动的问题,我自定义一个TextView,重写isFocused方法设置为true

public class MarqueeTextView extends TextView {
    public MarqueeTextView(Context context) {
        super(context);
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeTextView(Context context, AttributeSet attrs,
                           int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}
跑马灯布局如下

<com.spark.huabang.view.MarqueeTextView
    android:id="@+id/get_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="35dp"
    android:layout_marginLeft="10dp"
    android:ellipsize="marquee"
    android:focusable="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:singleLine="true"
    android:text="中奖名单:"
    android:textColor="@android:color/white"
    android:textSize="16sp">

</com.spark.huabang.view.MarqueeTextView>


三、现在跑马灯的TextView的数据,我假设有三段字符串构成:

Textview.setText(s);

s=s1+s2+s3;

此时,要想设置第二段字符串的颜色为红色,可以使用spannableStringBuilder的方法:

如果对首先用s+=s1;然后用

SpannableStringBuilder style = new SpannableStringBuilder(s);
style.setSpan(new ForegroundColorSpan(Color.RED), 0, s2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
最后使用s=s+s2+s3;TextView.setText(s);运行后会发现没反应,此时应该这么做才是正确的:
SpannableStringBuilder style = new SpannableStringBuilder(s);
style.setSpan(new ForegroundColorSpan(Color.RED), s.length()-s2.length, 
s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView.setText(style);

此时就可以设置完成了


http://www.niftyadmin.cn/n/3649182.html

相关文章

PYthon 转换HTML到Text纯文本

今天项目需要将HTML转换为纯文本&#xff0c;去网上搜了一下&#xff0c;发现Python果然是神通广大&#xff0c;无所不能&#xff0c;方法是五花八门。。。 拿今天亲自试的两个方法举例&#xff0c;以方便后人&#xff1a; 方法一&#xff1a; 1. 安装nltk&#xff0c;可以去…

如何使用vue-router设置Vue.js身份验证和路由处理

介绍 (Introduction) Vue.js is a progressive JavaScript framework for building front-end applications. Coupled with vue-router, we can build high performance applications with complete dynamic routes. Vue-router is an efficient tool and can efficiently hand…

Delphi之东进模拟语音卡(D160A)可复用源码

Delphi之东进模拟语音卡(D160A)可复用源码作者&#xff1a;成晓旭Blog&#xff1a;http://blog.csdn.net/cxxsoft(声明&#xff1a;欢迎转载&#xff0c;请保证文章的完整性)设计简介&#xff1a;1、 将卡、通道分别单独进行设计与封装。2、 所有的外部操作接口都封装在卡类…

友盟多渠道打包

1.按照umeng的要求&#xff0c;manifest文件中需要有 这段配置&#xff0c;value那里就是wandoujia&#xff0c;360之类的渠道名称&#xff0c;但是我们在这里不会去写渠道名&#xff0c;写的是一个占位符&#xff0c;后面gradle编译的时候会动态的替换掉它。 <meta-dataa…

Django出现的'ascii' codec can't encode characters in position...的解决办法

昨天买了服务器空间&#xff0c;由于服务器在国外&#xff0c;操作系统是英文版的Ubuntu11&#xff0c;多多少少会遇到编码的问题 今天遇到的问题是上传一个带有中文名的照片的时候&#xff0c;出现了以下错误&#xff1a;“ascii codec cant encode characters in position 5…

ios pusher使用_如何使用Laravel和Pusher通道创建Web通知

ios pusher使用Many web applications include an in-app notification system that will notify you instantly when someone carries out an action related to you or your account. On Facebook, you will be notified when someone likes your status, or when someone co…

有些像穴道被打通之前的周星驰

借用别人的一句话(http://ma-yue.net/index.php?p222)&#xff1a;有时候我觉得我们都有些像《功夫》中穴道被打通之前的周星驰&#xff1a;嗓门很大&#xff0c;梦想很多&#xff0c;但是除了开锁之技&#xff0c;却没有什么真正可以服人的“功夫”。不练会如来神掌之类的硬功…

在django中使用memcache

1&#xff0c;安装memcachesudo apt-get install memcached2,安装python的memcached支持wget ftp://ftp.tummy.com/pub/python-memcached/python-memcached-latest.tar.gz解压&#xff0c;进入目录python setup.py install3,配置memcached由于memcached不能用root帐号启动服务&…