现在的位置: 首页 > 综合 > 正文

Android入门/TextView控件(四)

2018年05月26日 ⁄ 综合 ⁄ 共 4313字 ⁄ 字号 评论关闭

概述内容:

1. 
使用
spannableStringBuilder为TextView的字体设置诸如粗体,斜体,前景色,背景色,字体大小,字体风格等等

2.     在XML中设置超链接

3.    
跑马灯
的样式的示例

TextView:

开发者通过使用TextView控件,将文本信息展示给用户。

这里使用xml布局而不是使用java硬编码,虽然java硬编码并不被提倡。

但实际上,我们项目中经常需要动态的创建TextView,所以我们仍需要掌握它的Java编码方式


Now,我们首先实现一个布局,这将示例我们如何高亮和背景

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#4399f0"
    android:layout_height="match_parent" >

   <TextView
   	   	 android:id="@+id/tv_main"
   	   	 android:layout_width="fill_parent"
   	   	 android:layout_height="wrap_content"
   	  	 android:text="@string/app_name"
   	   />
   <TextView
   	     android:id="@+id/tvv_main"
   	   	 android:layout_width="fill_parent"
   	   	 android:layout_height="wrap_content"
   	   	 android:text="@string/hello_world"
   	   	 android:textColor="#123456"
   	   	 android:textSize="30sp"
   	   	 android:layout_below="@id/tv_main"
   	   	
   	   />
</RelativeLayout>

xmlns:tools :它的作用_______________________________________________________________

android : layout_width  设置控件本身的宽度 , 可以设置为wrap_content(根据内容填充) 或者  fill_parent(填充满父类的宽度)

android : layout_height 设置控件本身的高度,同样可以设置为wrap_content 或 fill_parent

android : text  设置文本 ,文本的值在values/string.xml文件中,我们可以打开string.xml中查看app_name这个声明

android :texColor 设置文本颜色   , textSize设置文本大小 。 我们通常使用sp来调整字体大小,而不是dp或dip或px。

android:layout_below 目的使相对布局下的tv_main控件的下方显示tvv_main这个控件

现在我们写Activity类 ,如果你还不了解Activity类,请点击Activity生命周期

public class MainActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        TextView textview=(TextView)findViewById(R.id.tvv_main);
        String strs = textview.getText().toString();
                
        SpannableStringBuilder style=new SpannableStringBuilder(strs);

        style.setSpan(new BackgroundColorSpan(Color.RED),4,7,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.BLUE),0,3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        /*     
    		SPAN_EXCLUSIVE_EXCLUSIVE : 即在原文本头或尾追加新文本的样式不受原文本样式影响,原文本高亮,新追加文本不高亮。 
			SPAN_EXCLUSIVE_INCLUSIVE : 即在原文本尾追加新文本的样式受原文本样式影响,原来文本尾高亮,新追加文本也高亮。
         */   
        textview.setText(style); 
    }

}

通过setContentView()方法 将布局文件activity_main.xml 显示出来 >>

通过findViewById()找到预先在XML文件中定义的控件,这样以便于在java中修改属性或设置各种事件等。

关于SpannableStringBuilder,请参考 SpannableStringBuiler详解他这里写的非常赞 ~

我们通过
spannableStringBuilder
为TextView的字体设置诸如粗体,斜体,前景色,背景色,字体大小,字体风格等等


现在我们run app看下运行结果  我们为整个布局设置背景色为#4399f0。

另外我显示了我的app_name,它被准备定义在string.xml里。

另外我高亮了字符0~3,蓝色的YOU,另外红色背景了4~7字符,我想这应该会相当有用。

另外设置其他属性可以参考SpannableStringBuiler详解

After  现在我们可以漂亮的展示出各种样式的字符,那么超链接呢?现在我们就学习,如何在Android中展示超链接样式。

现在我们创建一个新的布局文件,把他命名为textview.xml 。需要注意的是,在layout下,不允许以大写字符命名XML文件(也不允许以纯数组或者数字开头),如果你那样命名会引起错误警告,造成R.java生成不了。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   	 
   	    <TextView
   	     android:autoLink="all"
   	   	 android:layout_width="fill_parent"
   	   	 android:layout_height="wrap_content"
   	   	 android:text="@string/myblogurl"
   	   	 android:textColor="#d56232"
   	   	 android:textSize="18sp"
</RelativeLayout> 
   	  

还有不要忘记修改Activity中的setContentView()方法,特别是你需要findViewById某个子Widget的时候,

如果你忘记了,程序会提醒你 nullPoint错误

        setContentView(R.layout.textview);
现在我们重新run app ,这里 包括网址,电话,Email都加上了超链接。

android:autoLink="all"
可选值有none/web/email/phone/map/all。

Last 我们展示一个”跑马灯“

我们修改XML文件

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   
   	    <TextView
   	   	 android:layout_width="fill_parent"
   	   	 android:layout_height="wrap_content"
   	   	 android:text="@string/long_string"
   	   	 android:textSize="18sp"

   	   	 
   	   	 android:ellipsize="marquee"
   	   	 android:focusable="true"
   	   	 android:marqueeRepeatLimit="marquee_forever"
   	   	 android:focusableInTouchMode="true"
   	   	 android:scrollHorizontally="true"
   	   	 android:shadowColor="#ff0000"
   	   	 android:shadowRadius="3.0"
   	   />  
</RelativeLayout>

android:allipsize=”marquee“ 是设置为跑马灯样式

android:focusable=”true“设置为焦点可获取

android : marqueeRepeatLimit = "marquee_forever" 无限循环

android : shadowRadius ="3.0" 是设置阴影距离3.0 这样效果会好一些

android : shadowColor = "#ff0000" 设置阴影颜色

更多TextView API 信息可以参考TextView2.2 中文API

让我们运行一下结果 此时出现了意料之外的问题.. 跑马灯不跑了!? 难道是马是没吃饱?!

因为跑马灯是适用于字符串超长的情况下, 那么我们把字符串修改的长一些 ?

"跑马灯" 果真跑了起来,并且开始循环的输出。


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------




抱歉!评论已关闭.