`
h389301776
  • 浏览: 9220 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Android程序的基本控件使用之文本显示组件:TextView

 
阅读更多

Android程序的基本控件使用之文本显示组件:TextView

文本显示组件:TextView

 

掌握文本显示组件的配置及使用

掌握文本显示组件的继承结构

掌握文本显示组件的基本属性及操作方法

 

内容:

 对于文本组件而言主要的目的是静态的显示一些文字,就想到于完成了一些标签的显示功能。

  Android.Widget. TextView类时View类的直接子类,所以在本组件之中也会提供更多的操作方法及配置的相关属性:

Java.Lang.Object

  Android.View.View

     Android.Widget.TextView

 

 

 

下面新建一个项目

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:orientation="vertical"

              android:layout_width="fill_parent"

              android:layout_height="fill_parent"

        >

    <TextView  定义文本组件

            android:id="@+id/mytext1" 定义组件ID

            android:layout_width="fill_parent" 组件宽度为屏幕宽度

            android:layout_height="wrap_content"  组件高度为文字高度

            android:textColor="#FFFF00"  //字体设置黄色

            android:textSize="12px"   //字的大小为12像素

            android:text="广西你好!"  设置显示文字

            />

</LinearLayout>

 

对于文本的大小必须有一个大小的单位,而px表示像素。

 

 

Px(pixels):像素

Dip(decice independent pixels):依赖于设备的像素

Sp(scaled pixels—best for text size):带比例的像素

Pt(points):

In(inches):英尺

Mmmillimeters:毫米’’

<TextView

            android:id="@+id/mytext2"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

 

            android:layout_margin="30px"  表示距离上下有30个像素

            android:text="网址:www.baidu.com"

            />

 

 

 

<TextView

            android:id="@+id/mytext2"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

 

            android:layout_marginTop="10px"

            android:text="徐辉"

       

            />

 

 

 

 

<TextView

            android:id="@+id/mytext2"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

 

            android:layout_marginTop="10px"  距离上面有10个像素

            android:text="徐辉"   默认显示文字

            android:maxLength="1" 最多只显示1个文字

            />

 

 

 

<TextView

            android:id="@+id/mytext4"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:background="@drawable/xuhui"

            android:textColor="#FFFF00"

            android:text="这是在背景上的信息徐辉"

 

            />

 

 

<TextView

        android:id="@+id/mytext4"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:background="@drawable/xuhui"

        android:textColor="#000000"    黑色字体

        android:textStyle="bold"    字体改为粗体

        android:text="这是在背景上的信息徐辉"

 

        />

 

这些就是在文本显示上的一些变化,而对于文本的显示风格有以下几种

设置文字的显示风格(android:textStyle):

正常(normal粗体(bold倾斜(italic)

所有组件在R.java中生成(我这没有)

 

以上只是进行了一些基本的文本信息的提示功能的实现,在Android之中,文本的功能不止如此,在Android之中如果文本显示组件上出现了一些网址信息也可以将其变为链接的形式出现。

<TextView

            android:id="@+id/msg"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:autoLink="all" 里面的链接内容自动变为地址链接

            android:textColor="#FFFF00"

            android:textSize="45px"

            android:text="网址: www.baidu.com"

            />

 

 

了解了基本使用之后,现在也会出现一个问题,如果说要在一个项目之中定义多个文本组件,那么这可能会存在着许多重复的配置属性,这样的话对于开发维护很麻烦,所以在Android操作系统之中也可以使用样式表文件进行统一的属性配置。

 

范例:定义一个样式表—styles.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <style name="msg_style">  定义样式表的配置,其中“msg_style”为名字

        <item name="android:textSize">45px</item>  定义文本大小

        <item name="android:textColor">#FFFF00</item> 定义文本颜色

        <item name="android:autoLink">all</item>  定义链接显示文字

        <item name="android:layout_height">wrap_content</item>  组件的高度

        <item name="android:layout_width">fill_parent</item>  组件的宽度

 

    </style>

 

</resources>

 如果要想引用样式表文件

main

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:orientation="vertical"

              android:layout_width="fill_parent"

              android:layout_height="fill_parent"

        >

    <TextView

            android:id="@+id/msg" 组件ID

            style="@style/msg_style" 定义组件显示的样式风格

            android:text="网址: www.baidu.com"

            />

 

</LinearLayout>

 

通过此种方式的显示更加方便于系统的维护操作,对于文本显示真正使用的就是标签的提示信息。

 

总结:

 

TextView作为文本组件主要的功能是显示文本数据;

所有的组件可以直接通过一个样式表文件进行属性的配置

分享到:
评论

相关推荐

    Android基本控件(上)源码

    文本显示组件:TextView,按钮组件:Button,编辑框:EditText,单选钮,复选框,下拉列表框:Spinner,图片视图:ImageView,0图片按钮:ImageButton,时间选择器:TimePicker,日期选择器:DatePicker

    RichTextView:一个基于Android原生TextView的富文本组件,解析Html格式的文本内容并进行显示

    RichTextView一个基于Android原生TextView的富文本组件,解析Html格式的文本内容并进行显示

    Fragment获取父Activity的TextView控件并修改内容

    Fragment获取父Activity的TextView控件并修改内容。这是一个demo,简单实现fragment和activity之间的交互。

    android常用控件综合应用

    Android常用控件的声明 TextView:文本显示框 EditView:文本编辑框 Button:按钮 Menu:菜单 RadioButton:单选按钮 RadioGroup:单选按钮组 CheckBox:复选框 ScrollView:滚动条

    Android 中常见控件的介绍和使用.rar_adroid_android

    Android 中常见控件的介绍和使用,TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域。

    Android核心技术与实例详解 PDF扫描版

    本书以Android应用程序的开发为主题 并结合真实的案例向读者详细介绍了Android的基本组件的使用及应用程序开发的整个流程 全书分为三篇共18章 第一篇以简单易懂的实例为依托 详细介绍了Android平台的基本控件 高级...

    安卓java读取网页源码-Android_Assembly_Summary:对Android各组件分类汇总

    一款支持TextView文字动画效果的Android组件库。 ScrollNumber 滚动数字控件 ticker 滚动数字控件 ReadMoreTextView 阅读更多,折叠文本 ExpandableTextView 折叠文本 android-autofittextview 自动调整文字大小 ...

    android开发入门与实战(下)

    第7章 良好的学习开端——Android基本组件介绍 7.1 第一印象很重要——界面UI元素介绍 7.1.1 视图组件(View) 7.1.2 视图容器组件(Viewgroup) 7.1.3 布局组件(Layout) 7.1.4 布局参数(LayoutParams) 7.2 我的美丽我...

    Android编程入门很简单.(清华出版.王勇).part1

    5.2.1 使用可滚动的文本控件——TextView 5.2.2 TextView中的一些功能 5.2.3 使用可滚动的视图——ScrollView 5.2.4文字的编辑 5.2.5 使用按钮——Button 5.2.6实例——计算器 5.2.7 使用图片按钮——ImageButton ...

    新版Android开发教程.rar

    • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • 集成的浏览器 基于开源的 WebKit 引擎 • 优化的图形库 包括定制的 2D 图形库, 3D 图形库基于 OpenGL ES 1.0 ...

    Android编程入门很简单.(清华出版.王勇).part2

    5.2.1 使用可滚动的文本控件——TextView 5.2.2 TextView中的一些功能 5.2.3 使用可滚动的视图——ScrollView 5.2.4文字的编辑 5.2.5 使用按钮——Button 5.2.6实例——计算器 5.2.7 使用图片按钮——ImageButton ...

    【android编程】 第四讲-Android基本控件

    文章目录Android 基本控件文本框 TextView基本属性其他属性编辑框 EditText普通按钮 Button图片按钮ImageButton单选按钮 RadioButton复选按钮 CheckBox图像视图 ImageView使用私有对象响应按钮点击题目笔记 ...

    Android开发案例驱动教程 配套代码

    《Android开发案例驱动教程》 配套代码。 注: 由于第12,13,14章代码太大,无法上传到一个包中。 这三节代码会放到其他压缩包中。 作者:关东升,赵志荣 Java或C++程序员转变成为Android程序员 采用案例驱动模式...

    Android开发应用实战详解源代码

    1.1.3 android组件结构 1.1.4 android应用程序框架 1.1.5 android的竞争优势 1.1.6 android模拟器 1.2 搭建android开发环境 1.2.1 准备工作 1.2.2 windows系统下的搭建过程 1.2.3 linux系统下的搭建过程 1.3 常见...

    android实习报告(1).doc

    2.Android图形设计UI Android UI控件一般写在布局文件中,此次实习所学控件主要有:TextView,EditText Button 、AlertDialog、RadioButton、CheckBox、Spinner和TableRow等。将这些控件及相应的 属性写在layout下...

    Android开发中Button组件的使用

     安卓系统中,Button是程序和用户进行交互的一个重要控件,今天我们就来简单的对Button进行学习,其中Button组件是文本按钮(继承自TextView),而ImageButton是图像按钮(继承自ImageView)。两者之间的区别在于: ...

    Android基础知识详解

    Android控件的继承关系 22 一、View与ViewGroup关系 22 二、各控件的继承关系 23 界面布局 25 LinearLayout(线性布局) 25 TableLayout(表格布局) 28 RelativeLayout(相对布局) 31 AbsoluteLayout(绝对布局) 34 ...

Global site tag (gtag.js) - Google Analytics