Android TextView页面布局源代码

来源:互联网 发布:氰化物淘宝有卖么 编辑:程序博客网 时间:2024/06/04 18:08

跟大家介绍android源代码关于TextVie的几个简单效果和功能

一.TextView显示自我介绍

有关TextView的属性大家可以看这里:http://bbs.9tech.cn/topic-364380-1.html

textview这个控件就是用来显示文字的。我们在Eclipse中打开上一节建立的工程part1(大家也可以新建一个part2都是可以的)。

1.打开text_view.xml 我们来做第一个布局页面

这里跟大家说声,为了自己的应用程序能在更多的手机上使用,一般我们选用的SDK是2.1,我的教程里面是用SDK2.2的。

2.xml的开头写法

有的Eclipse里的ADT版本不同写法也不同,所以我采用一种通用的方法。

<、?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">

<、!--在这里添加控件-->

<、/LinearLayout>

其中:

1android:orientation="vertical"<、span style="font-size:16px;font-family:'sans serif', tahoma, verdana, helvetica;line-height:1.5;"> 这个属性是设置你布局里的控件是要垂直显示的,horizontal值是水平显示。<、/span>

我们要添加的控件是在节点下的。节点开始是<>以作为结束。每对节点都是有头有尾的,里面在包含的节点我们称之为他的子节点。

text_view.xml 代码:

<、?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/name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="姓名:Android淘气小公主"

android:textColor="#000000"

android:textSize="20sp"/>

<、TextView

android:id="@+id/sex"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="性别:女"

android:textColor="#000000"

android:textSize="20sp"/>

<、TextView

android:id="@+id/age"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="年龄:24"

android:textColor="#000000"

android:textSize="20sp"/>

<、TextView

android:id="@+id/xz"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="星座:巨蟹座"

android:textColor="#000000"

android:textSize="20sp"/>

<、TextView

android:id="@+id/xx"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="血型:O型"

android:textColor="#000000"

android:textSize="20sp"/>

<、TextView

android:id="@+id/boke"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="博客:http://blog.9tech.cn/emaoer"

android:textColor="#000000"

android:textSize="20sp"/>

<、TextView

android:id="@+id/weibo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="微博:@Android淘气小公主"

android:textColor="#000000"

android:textSize="20sp"/>

<、TextView

android:id="@+id/work"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="工作:Android自由开发者"

android:textColor="#000000"

android:textSize="20sp"/>

<、/LinearLayout>

3.在TextViewActivity.java文件里我们要做的

首先,我们要知道程序在创建Activity的时候就要走他的onCreat()方法,所以我们要添加他的onCreat()方法。

然后我们要在他的方法里去声明控件的id,这点很总要。尤其,是要监的控件必须声明。Android是有R文件的,R文件里的东西我们是不可以修改的。

package com.tech.part2;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class TextViewActivity extends Activity {

private TextView name,sex,age,xz,xx,weibo,boke,work;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.text_view);//此方法是声明你的布局文件

//注册声明控件

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

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

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

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

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

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

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

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

}

}

二 TextView超链接功能

1.把地址直接编程超链接

在TextView控件属性中,添加android:autoLink="all"属性。

2.文字添加超级链接

我们可以用SpannableString这个类来帮我们完成任务。方法如下:

SpannableString spStr = new SpannableString(weibo.getText().toString());

spStr.setSpan(new URLSpan("http://weibo.com/emaoer520"), 0, weibo.getText().toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

weibo.setText(spStr); //修改textview内容

weibo.setMovementMethod(LinkMovementMethod.getInstance());

显然这个还不是理想效果的如何,从截图来看大家可以看到,博客下面也划线了。怎样处理呢?我们来用正则表达式吧。

SpannableString spStr = new SpannableString(weibo.getText().toString());

Pattern pattern = Pattern.compile("@([^>]*?s)|@([^>]*)");

Matcher matcher = pattern.matcher(spStr);

while (matcher.find()) {

spStr.setSpan(new URLSpan("http://weibo.com/emaoer520"), matcher.start(),matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

weibo.setText(spStr);

weibo.setMovementMethod(LinkMovementMethod.getInstance());

更多关于android源代码的信息,可查询天地会http://android.9tech.cn/

0 0
原创粉丝点击