TextView使用

public class MainActivity extends Activity {

TextView tv1, tv2, tv3, tv4, tv5;

    String s, s2, s3, s4, s5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    private void initData() {
        // TODO Auto-generated method stub
        //实例1,textView点击超链接
        s="主页:https://www.baidu.com\n";
        s+="个人邮箱:123456789@qq.com\n";
        s+="电话:029-12345678";
        tv1.setText(s);
        
        //实例2,textView显示Html
        s2="<font code='red'>我的浏览器主页</font><br>";
        s2+="<a href='https://www.baidu.com'>百度</a>";
        Spanned spanned= Html.fromHtml(s2);
        tv2.setText(spanned);
        tv2.setMovementMethod(LinkMovementMethod.getInstance());
        
        //实例3,使用html插入图片
        s3="图片1:<img src='s1'/><br>";
        s3+="图片2:<img src='s2'/><br>";
        s3+="图片3:<img src='s3'/>";
        
        Spanned s2=Html.fromHtml(s3, new ImageGetter(){

            @Override
            public Drawable getDrawable(String arg0) {
                Drawable d=null;
            try {
                
                Field f=R.drawable.class.getField(arg0);
                int i=Integer.parseInt(f.get(null).toString());//这里是将图片的信息先转换成string,在转换成int
                d=getResources().getDrawable(i);
                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());//调用方法,使图片显示出来;最后两个参数的意思是图片的宽和高
                
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
                return d;
            }}, null);
        
        tv3.setText(s2);
        
        //实例4:部分文字点击
        s4="点击【这里】显示Toast";
        //拆分器
        SpannableString ss=new SpannableString(s4);
        //第一个参数代表:拆分出来的干什么用,第二个参数、第三个参数代表拆分的那部分,第四个参数是拆分器的选项
        ss.setSpan(new ClickableSpan() {
            
            @Override
            public void onClick(View arg0) {
                Toast.makeText(MainActivity.this, "您点击了这里", Toast.LENGTH_SHORT).show();
            }
        }, 3, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#00ff00")), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv4.setText(ss);
        tv4.setMovementMethod(LinkMovementMethod.getInstance());
        //实例5:走马灯效果
        /*
         * android:singleLine="true"单行显示
         * android:ellipsize="marquee"当文字长度超过textview宽度时的显示方式,这里是当超过屏幕宽度的时候,后面用省略号显示
         *  android:marqueeRepeatLimit="marquee_forever"滚动的次数
         *  android:focusable="true" 可以聚焦的,
         *  android:focusableInTouchMode="true" 可以聚焦的
         */
        s5=".setMovementMethod,此方法在需要响应用户事件时使用,如点击一个电话号码就跳转到拨号页面。如果不执行这个方法是不会响应事件的,即便文本看着已经是下划线蓝色字了。 .Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,这是在 setSpan 时需要指定的 flag,它的意义我试了很久也没试出来,睡个觉,今天早上才突然有点想法,试之,果然。它是用来标识在 Span 范围内的文本前后输入新的字符时是否把它们也应用这个效果。分别有 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)、Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)、Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)、Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)";
        tv5.setText(s5);
    }

    private void initView() {
        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv4 = (TextView) findViewById(R.id.textView4);
        tv5 = (TextView) findViewById(R.id.textView5);

    }

只是布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="all"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
         />

</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Spannable 实现 TextView 的颜色渐变效果,你可以使用 ForegroundColorSpan 类来设置不同部分的字体颜色,并根据需要调整它们的位置和颜色值。 以下是一个示例代码,演示如何使用 Spannable 实现颜色渐变效果: ```java TextView textView = findViewById(R.id.textView); String text = "Hello World!"; Spannable spannable = new SpannableString(text); // 定义渐变色数组 int[] colors = {Color.RED, Color.GREEN, Color.BLUE}; // 定义颜色变化位置数组 float[] positions = {0f, 0.5f, 1f}; for (int i = 0; i < text.length(); i++) { // 计算当前字符的颜色 int color = interpolateColor(colors, positions, (float) i / (text.length() - 1)); // 创建 ForegroundColorSpan,并设置字体颜色 ForegroundColorSpan span = new ForegroundColorSpan(color); // 设置 span 的起始位置和结束位置 spannable.setSpan(span, i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } // 将 spannable 设置给 TextView textView.setText(spannable); ``` 在这个示例中,我们首先创建了一个 SpannableString 对象,并将其初始化为需要处理的文本。然后,我们使用一个循环遍历文本中的每个字符,计算出当前字符的颜色值,然后创建一个 ForegroundColorSpan 对象,并将其应用到对应的字符上。最后,我们将处理后的 SpannableString 设置给 TextView。 在 interpolateColor 方法中,我们使用了一个插值算法来计算颜色的渐变值。你可以根据需要自定义这个方法,实现不同的颜色渐变效果。 这样,TextView 的文本就会呈现出颜色渐变的效果。你可以根据实际需求和喜好,调整渐变色数组、颜色变化位置数组以及插值算法来实现不同的颜色渐变效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值