美文网首页
Android - 学习笔记五 - TextView 使用

Android - 学习笔记五 - TextView 使用

作者: 精神病患者link常 | 来源:发表于2018-02-02 16:09 被阅读11次

1.显示html

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

public class MainActivity extends AppCompatActivity {

    TextView textView1;

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

        textView1 = (TextView)findViewById(R.id.textView1);
     

        //设置需要显示的字符串
        String html="<font color ='red'>Hello android</font>";
        html+="<font color='#0000ff'><big><i>Hello android</i></big></font>";
        html+="<big><a href='http://www.baidu.com'>百度</a></big>";

        //使用Html.fromHtml,把含HTML标签的字符串转换成可显示的文本样式
        CharSequence charSequence=Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);

        textView1.setText(charSequence);
        
        //设置html的字符串可以link,这样点击上面的百度就可以跳转到浏览器
        textView1.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

2.识别链接,phone、email、web等;长按可以选择文字

<TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/textView2"
           android:autoLink="all" // 默认全部可以link
           android:textIsSelectable="true"/> // 长按选择文字

3.文字很长时,可以上下滚动

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp" // 给一个高度
        android:id="@+id/textView3"
        android:scrollbars="vertical"
        android:layout_below="@+id/hig"
        android:layout_marginTop="100dp"/>
// 设置滚动
textView3.setMovementMethod(ScrollingMovementMethod.getInstance());

4.跑马灯

<TextView
        android:id="@+id/textview11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="这是一片很寂寞的天下着有些伤心的雨"
        android:focusableInTouchMode="true"
        android:singleLine="true"/> // 已废弃......
 TextView textView11 = (TextView) findViewById(R.id.textview11);
 textView11.setSelected(true);

相关文章

网友评论

      本文标题:Android - 学习笔记五 - TextView 使用

      本文链接:https://www.haomeiwen.com/subject/jglezxtx.html