安卓常用控件————标签

TextView是View的子类,用于显示文本信息,而EditText都是TextView的子类,用于接收用户输入的数据。

添加四个标签,显示四种不同样式

设置内容、字体颜色、字号、样式

对应的属性:text、textColor、textSize、textStyle

(1)修改布局文件activity_main.xml

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

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#000000"

android:orientation="vertical"

android:padding="10dp" >



<TextView

android:id="@+id/tv_normal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/normal"

android:textColor="@android:color/holo_blue_bright"

android:textSize="15sp"

android:textStyle="normal" />



<TextView

android:id="@+id/tv_bold"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/bold"

android:textColor="@android:color/holo_green_light"

android:textSize="15sp"

android:textStyle="bold" />



<TextView

android:id="@+id/tv_italic"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/italic"

android:textColor="@android:color/holo_orange_light"

android:textSize="15sp"

android:textStyle="italic" />



<TextView

android:id="@+id/tv_bold_italic"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/bold_italic"

android:textColor="@android:color/holo_purple"

android:textSize="15sp"

android:textStyle="bold|italic" />



</LinearLayout>

(2)修改strings.xml文件

<resources>

<string name="app_name">0601Demo</string>

<string name="action_settings">设置</string>

<string name="normal">常规:Welcome to Android World</string>

<string name="bold">粗体:Welcome to Android World</string>

<string name="italic">斜体:Welcome to Android World</string>

<string name="bold_italic">粗斜:Welcome to Android World</string>

</resources>

运行程序,看看效果:

任务2、添加分隔线,实现分组效果

在实际项目开发中,经常会用到水平或垂直分割线,达到界面逻辑分块的目的。

<!--分割线-->

<View

android:layout_width="match_parent"

android:layout_height="1dp"

android:layout_marginBottom="5dp"

android:layout_marginTop="5dp"

android:background="#aaaaaa" />

运行效果:

任务3、添加一个标签,将所有字母自动转换成大写

(1)修改activity_main.xml

添加:

<TextView

android:id="@+id/tv_all_caps"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/all_caps"

android:textAllCaps="true"

android:textColor="#ffffff"

android:textSize="15sp"

android:textStyle="normal" />

(2)修改strings.xml

添加:

<string name="all_caps">全大写:Welcome to Android World</string>

此时的运行效果:

任务4、实现上标与下标(实现网页的某些效果)

(1)在布局文件里添加两个标签

<TextView

android:id="@+id/tv_theorem"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:text="@string/theorem"

android:textColor="@android:color/holo_red_light"

android:textSize="20sp" />



<TextView

android:id="@+id/tv_equation"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:text="@string/equation"

android:textColor="@android:color/holo_green_dark"

android:textSize="20sp" />

(2)修改strings.xml

添加:

<string name="theorem">勾股定理:</string>

<string name="equation">化学方程式:</string>

(3)修改MainActivity

要通过Java代码实现,利用格式转换类Html提供的fromHTML方法来处理含有html标签的字符串。

此时的运行效果:

其实大家还可以尝试其它HTML标签,查看效果。比如样式标签<b>与<i>、标题标签<h1>....<h6>……

任务5:自动链接:网页、邮箱、电话(一般用于设置联系方式)

属性 autoLink,取值:none、web、email、phone、map

在activity_main.xml里添加:

<!-- 分隔线 -->

<View

android:layout_width="match_parent"

android:layout_height="1dp"

android:layout_marginBottom="5dp"

android:layout_marginTop="5dp"

android:background="#aaaaaa" />



<TextView

android:id="@+id/tv_web"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:autoLink="web"

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

android:textColor="@android:color/holo_orange_light"

android:textSize="15sp" />



<TextView

android:id="@+id/tv_email"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:autoLink="email"

android:text="邮箱: maths007@163.com"

android:textColor="@android:color/holo_green_light"

android:textSize="15sp" />



<TextView

android:id="@+id/tv_phone"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:autoLink="phone"

android:text="电话: 15892921170"

android:textColor="@android:color/holo_red_light"

android:textSize="15sp" />

此时的运行效果:

比如,单击网址之后的超链接,立马自动访问该网页:

单击电话之后的超链接,自动拨打电话:

任务6、添加有边框和渐变色的文本视图

(1)在res里创建drawable目录,在里面创建custom_border.xml文件。

(2)在布局资源文件里添加一个标签

<!-- 分隔线 -->

<eView

android:layout_width="match_parent"

android:layout_height="1dp"

android:layout_marginBottom="5dp"

android:layout_marginTop="5dp"

android:background="#aaaaaa" />



<TextView

android:id="@+id/tv_bordered_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:background="@drawable/custom_border"

android:text="带边框的文本:永不放弃的梦想"

android:textColor="#ff0000"

android:textSize="15sp" />

此时,运行效果:

现在我们来看看在代码里如何设置控件属性。

格式:控件名.set属性名(属性值);

任务7、设置【化学方程式】文本视图的字号与颜色。

此时,运行效果如下:

标签常用属性

1、text:标签文本内容

2、textColor:标签文本颜色(#fff、@color/red)

3、textSize:标签文本字号(20sp)

4、textStyle:标签文本样式(normal | bold | italic)

5、autoLink:自动链接(none、web、email、phone、map)

6、background:背景(背景色、背景图片、背景图形)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义控件是Android开发常见的任务之一。下面是一步一步教你如何自定义控件的简要指南: 第一步:创建一个新的Java类作为你的自定义控件。 首先,创建一个新的Java类,可以命名为你想要的控件名称。这个类应该继承自Android框架的现有控件,例如View、TextView等。例如,如果你想要创建一个自定义按钮,可以创建一个名为CustomButton的类,并让它继承自Button类。 第二步:实现构造函数和属性。 在你的自定义控件,你可以实现构造函数和属性,以便对控件进行初始化和设置。你可以定义自己的属性,例如颜色、大小等,以及相应的getter和setter方法。 第三步:重写绘制方法。 要自定义控件的外观,你需要重写它的绘制方法。最常用的方法是重写`onDraw()`方法,在其使用Canvas绘制你想要的形状、文本等。 第四步:处理用户交互。 如果你的自定义控件需要与用户进行交互,你可以重写相应的触摸事件(例如`onTouchEvent()`)或点击事件(例如`setOnClickListener()`)来处理用户操作。 第五步:在布局文件使用自定义控件。 完成以上步骤后,你可以在布局文件使用你的自定义控件了。只需在布局文件添加一个与你的控件类名相对应的XML标签,并设置相应的属性。 这只是一个简要的指南,帮助你开始自定义控件的过程。在实际开发,你可能需要更多的步骤和细节来完成你的自定义控件。你可以参考Android官方文档或其他教程来获取更多信息和示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值