Android学习:
TextView
activity_main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.kn_day03_1_textview.MainActivity" >
<!--
TextView的常用属性:
android:text 指定当前控件要显示的文字是什么
属性值的设置:
1.自定义的任意字符串
2.官方推荐 ,使用引用资源字符串的方式,即@string/资源名
android:textColor="" 控制字体颜色
android:textSize="" 控制字体显示的大小, 属性值的设置:数字+单位,单位推荐使用sp
android:textStyle 控制字体的样式,
属性值有3种:
normal 默认的样式
bold 加粗
italic 倾斜
当想要让特别长的文字显示省略提醒的时候,首先要设置TextView最多显示行数
实现方式有2种:
android:lines="3" 属性值指定最多显示多少行
android:singleLine="true" 设置单行显示
此两条属性的省略区别:
android:singleLine属性,仅显示一行,会默认在结尾处加...代表省略
android:lines 设置几行显示几行,但不会加...
控制单行显示时的省略号位置:当只有单行的时候才能控制...的提示位置,
即:android:singleLine="true"的前提下,设置android:ellipsize才会有效!
android:ellipsize 控制如果内容显示不下的时候,省略方式,以及省略的提示方式 有5个值:
none 无效果
start 省略内容的开头,并且在开始部分加...
middle 省略中段部分,并且在中间部分加...
end 省略后段部分,并且在结尾部分加...
marquee 以跑马灯的形式显示所有的内容,即滚动文字显示所有内容 仅 android:singleLine="true"时有效,因为单行才有滚动的需求
如果想要android:ellipsize="marquee"生效的话,必须同步匹配以下属性:
android:marqueeRepeatLimit="marquee_forever" 设置滚动次数,当前的属性代表永远滚
android:focusable="true" 设置获取焦点
android:focusableInTouchMode="true" 设置可以通过触摸的方式获取焦点
跑马灯效果需要这5条件实: android:singleLine="true"+android:ellipsize="marquee"+上面三个,控制滚动速度等属性尚不知是哪个
-->
<TextView android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColor="@color/abc_search_url_text_holo"
android:singleLine="true"
android:textSize="18sp"
android:textStyle="bold|italic"
android:text="@string/hello_world" />
<TextView android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1"
android:layout_marginTop="20dp"
android:lines="3"
android:text="Android的系统架构和其操作系统一样,采用了分层的架构。Android结构:从架构图看,Android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和Linux内核层。
Android开发四大组件分别是:活动(Activity): 用于表现功能。服务(Service): 后台运行服务,不提供界面呈现。广播接收器(BroadcastReceiver):用于接收广播。内容提供商(Content Provider): 支持在多个应用中存储和读取数据,相当于数据库。" />
<TextView android:id="@+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview2"
android:layout_marginTop="20dp"
android:singleLine="true"
android:text="Android的系统架构和其操作系统一样,采用了分层的架构。Android结构:从架构图看,Android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和Linux内核层。
Android开发四大组件分别是:活动(Activity): 用于表现功能。服务(Service): 后台运行服务,不提供界面呈现。广播接收器(BroadcastReceiver):用于接收广播。内容提供商(Content Provider): 支持在多个应用中存储和读取数据,相当于数据库。" />
<TextView android:id="@+id/textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview3"
android:layout_marginTop="20dp"
android:singleLine="true"
android:ellipsize="start"
android:text="Android的系统架构和其操作系统一样,采用了分层的架构。Android结构:从架构图看,Android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和Linux内核层。
Android开发四大组件分别是:活动(Activity): 用于表现功能。服务(Service): 后台运行服务,不提供界面呈现。广播接收器(BroadcastReceiver):用于接收广播。内容提供商(Content Provider): 支持在多个应用中存储和读取数据,相当于数据库。" />
<TextView android:id="@+id/textview5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview4"
android:layout_marginTop="20dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="Android的系统架构和其操作系统一样,采用了分层的架构。Android结构:从架构图看,Android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和Linux内核层。
Android开发四大组件分别是:活动(Activity): 用于表现功能。服务(Service): 后台运行服务,不提供界面呈现。广播接收器(BroadcastReceiver):用于接收广播。内容提供商(Content Provider): 支持在多个应用中存储和读取数据,相当于数据库。" />
<!-- 设置当前显示的文字具备一些自动连接的效果
如:手机号 android:autoLink="phone" 设置显示的文字具备拨号自动连接的特性
其他值:
none 无效果
web 针对网址的连接特性
email 针对邮箱地址
map 针对地图信息方面
all 以上所有
-->
<TextView android:id="@+id/textview6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview5"
android:layout_marginTop="20dp"
android:autoLink="web"
android:textSize="25sp"
android:text=" http://www.baidu.com" />
</RelativeLayout>
xmlns:tools=" http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.kn_day03_1_textview.MainActivity" >
<!--
TextView的常用属性:
android:text 指定当前控件要显示的文字是什么
属性值的设置:
1.自定义的任意字符串
2.官方推荐 ,使用引用资源字符串的方式,即@string/资源名
android:textColor="" 控制字体颜色
android:textSize="" 控制字体显示的大小, 属性值的设置:数字+单位,单位推荐使用sp
android:textStyle 控制字体的样式,
属性值有3种:
normal 默认的样式
bold 加粗
italic 倾斜
当想要让特别长的文字显示省略提醒的时候,首先要设置TextView最多显示行数
实现方式有2种:
android:lines="3" 属性值指定最多显示多少行
android:singleLine="true" 设置单行显示
此两条属性的省略区别:
android:singleLine属性,仅显示一行,会默认在结尾处加...代表省略
android:lines 设置几行显示几行,但不会加...
控制单行显示时的省略号位置:当只有单行的时候才能控制...的提示位置,
即:android:singleLine="true"的前提下,设置android:ellipsize才会有效!
android:ellipsize 控制如果内容显示不下的时候,省略方式,以及省略的提示方式 有5个值:
none 无效果
start 省略内容的开头,并且在开始部分加...
middle 省略中段部分,并且在中间部分加...
end 省略后段部分,并且在结尾部分加...
marquee 以跑马灯的形式显示所有的内容,即滚动文字显示所有内容 仅 android:singleLine="true"时有效,因为单行才有滚动的需求
如果想要android:ellipsize="marquee"生效的话,必须同步匹配以下属性:
android:marqueeRepeatLimit="marquee_forever" 设置滚动次数,当前的属性代表永远滚
android:focusable="true" 设置获取焦点
android:focusableInTouchMode="true" 设置可以通过触摸的方式获取焦点
跑马灯效果需要这5条件实: android:singleLine="true"+android:ellipsize="marquee"+上面三个,控制滚动速度等属性尚不知是哪个
-->
<TextView android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColor="@color/abc_search_url_text_holo"
android:singleLine="true"
android:textSize="18sp"
android:textStyle="bold|italic"
android:text="@string/hello_world" />
<TextView android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1"
android:layout_marginTop="20dp"
android:lines="3"
android:text="Android的系统架构和其操作系统一样,采用了分层的架构。Android结构:从架构图看,Android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和Linux内核层。
Android开发四大组件分别是:活动(Activity): 用于表现功能。服务(Service): 后台运行服务,不提供界面呈现。广播接收器(BroadcastReceiver):用于接收广播。内容提供商(Content Provider): 支持在多个应用中存储和读取数据,相当于数据库。" />
<TextView android:id="@+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview2"
android:layout_marginTop="20dp"
android:singleLine="true"
android:text="Android的系统架构和其操作系统一样,采用了分层的架构。Android结构:从架构图看,Android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和Linux内核层。
Android开发四大组件分别是:活动(Activity): 用于表现功能。服务(Service): 后台运行服务,不提供界面呈现。广播接收器(BroadcastReceiver):用于接收广播。内容提供商(Content Provider): 支持在多个应用中存储和读取数据,相当于数据库。" />
<TextView android:id="@+id/textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview3"
android:layout_marginTop="20dp"
android:singleLine="true"
android:ellipsize="start"
android:text="Android的系统架构和其操作系统一样,采用了分层的架构。Android结构:从架构图看,Android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和Linux内核层。
Android开发四大组件分别是:活动(Activity): 用于表现功能。服务(Service): 后台运行服务,不提供界面呈现。广播接收器(BroadcastReceiver):用于接收广播。内容提供商(Content Provider): 支持在多个应用中存储和读取数据,相当于数据库。" />
<TextView android:id="@+id/textview5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview4"
android:layout_marginTop="20dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="Android的系统架构和其操作系统一样,采用了分层的架构。Android结构:从架构图看,Android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和Linux内核层。
Android开发四大组件分别是:活动(Activity): 用于表现功能。服务(Service): 后台运行服务,不提供界面呈现。广播接收器(BroadcastReceiver):用于接收广播。内容提供商(Content Provider): 支持在多个应用中存储和读取数据,相当于数据库。" />
<!-- 设置当前显示的文字具备一些自动连接的效果
如:手机号 android:autoLink="phone" 设置显示的文字具备拨号自动连接的特性
其他值:
none 无效果
web 针对网址的连接特性
email 针对邮箱地址
map 针对地图信息方面
all 以上所有
-->
<TextView android:id="@+id/textview6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview5"
android:layout_marginTop="20dp"
android:autoLink="web"
android:textSize="25sp"
android:text=" http://www.baidu.com" />
</RelativeLayout>
************************************************************
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
/*
* TextView
* 用于显示文字的控件
* 并且android中的特点,想要在页面上展示某些东西,必须创建一个Activity的子类
* 用于处理此显示页面的所有逻辑
* 当想要在页面上展示文字的话,必须让页面的布局中包含一个TextView控件,通过此控件去展示文字
* */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.textview);
/*
* 通过的是findViewById方法,参数
* 对应的是要想获得的控件对象的id属性的值
*/
TextView tv =
(TextView)findViewById(R.id.textview1);
tv.setText("textview test");
//设置文字的字体大小
tv.setTextSize(55);
/*
* 设置文字颜色,
* 参数值的设置:
* 1.调用系统的Color类,在此类中存储了一些现成的颜色属性
* 2.调用资源文件中的颜色值,
* 注意:此处的int值对应的不是R总的那个颜色的16进制值
* 而是某颜色对应的数字值,获得方式:
*
getResourde.getColor(R.color.颜色名称)
*
* 颜色的资源文件的创建方式:
* res--values文件夹下,创建colors.xml文件
* 在文件中添加color标签,每一个标签对应一个颜色
* */
tv.setTextColor(getResources().getColor(R.color.green));
// tv.setTextColor(Color.BLUE);
//设置文字样式
/*
* setTypeface ,要求传递是一个Typefaced的类型
* 类型作用:系统的字体样式的封装类
* 对向的创建方式:
*
1.使用该类对应的静态属性,Typeface.DEFAULT 获取系统默认的字体样式
* Typeface.DEFAULT_BOLD 获取系统默认的字体加粗样式
* 2.如果想要设置字体的倾斜,通过指定create方法
* 参数一:设置此样式的名字
* 参数二:字体的特点
* */
tv.setTypeface(Typeface.DEFAULT);
tv.setTypeface(Typeface.DEFAULT_BOLD);
//
tv.setTypeface(Typeface.create("", Typeface.BOLD_ITALIC), Typeface.ITALIC);
tv.setTypeface(Typeface.create("Aril", Typeface.BOLD_ITALIC), Typeface.ITALIC);
}
}
colors.xml文件 :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="green">#00ff00</color>
<color name="red">#ff0000</color>
<color name="white">#ffffff</color>
<color name="gray">#dddddd</color>
</resources>