在Android初级教程(五)我们写了HelloAndroid
之后,一直觉得没有写半行代码对不起自己,所以本节,我们将在HelloAndroid
基础之上,进行与TextView
文字标签的第一次接触.在此例中,将会在Layout
中创建TextView
对象,并学会定义res/values/string.xml
里的字符串常数,最后通过TextView
的setText
方法,在预加载程序之初,更改TextView
文字.
首先看一下运行结果如下图:
首先"欢迎来到魏祝林的博客"这几个字是从什么地方来的呢,我们是在res->values->string.xml里面加了如下一句(黑体):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloAndroid!</string>
<string name="app_name">HelloAndroid</string>
<string name="textView_text">欢迎来到魏祝林的博客</string>
</resources>
而加载"欢迎来到魏祝林的博客"是在main.xml
(定义手机布局界面的)里加入的,如下面代码,其中我们闺将@string/hello
改成了@string/textView_text
.
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/textView_text"
/>
</LinearLayout>
这样我们运行HelloAndroid.java时,手机画面里将显示"欢迎来到魏祝林的博客"的欢迎界面,貌似我们又是没有写代码,只是在.xml加了一两行搞定,对习惯了编程的同学,感觉有点不适应.其实在HelloAndroid.java写代码也可以完全达到一样的效果.
在这里我们首先将main.xml回归到原样在原样的基础上加上一行见下方(黑体行)这里ID是为了在Java类里,找到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/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
在主程序HelloAndroid.java里代码如下:
package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
private TextView myTextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//载入main.xml Layout,此时myTextView:text为hello
setContentView(R.layout.main);
//使用findViewById函数,利用ID找到该TextView对象
myTextView = (TextView)findViewById(R.id.myTextView);
String welcome_mes = "欢迎来到魏祝林的博客";
//利用setText方法将TextView文字改变为welcom_mes
myTextView.setText(welcome_mes);
}
}
两种方法都可以达到一样的效果,不过我在此建议用第一种比较规范一点.这一节就到此为至!!下一节我们将讲一下Android五大布局.希望大家继续关注~
分享到:
相关推荐
android控件详解----TextView
Android-TextView-LinkBuilder-master,创建可点击的链接,博客附件,效果请查看博客相对应项目。
android下textview 显示html片段,可以显示文字和图片混排,图片可以是网络图片
android实现TextView文字部分渐变色;LinearGradientFontSpan SpannableString
TagTextView:包含标签的TextView
旋转TextView文字显示方向,可参考学习一下。
Android textview 文字中间加上图片
支持TextView默认支持的标签 支持自定义标签,接口类似Html.TagHandler 1 自定义标签: /** * 解析哈哈哈 */ public class SpanTagHandler implements HtmlTagHandler.TagHandler { private String fontColor =...
android TextView android TextView
Android流式TextView,类似文字标签。
android textview 文字排版 换行
Android-html-textview.zip,显示简单HTML内容的文本视图,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。
Android实现TextView文字滚动
android 文字图片混排,在一个TextView中显示文字穿插图片,文字变色效果.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
主要介绍了Android实现在TextView文字过长时省略部分或滚动显示的方法,结合实例形式分析了Android中TextView控件文字显示及滚动效果相关操作技巧,需要的朋友可以参考下
Android SpannableString实现TextView不同大小文字居中显示。
android 逐行逐字显示的textview 播放通知的字幕 逐字逐行显示。可以控制显示的次数。
The library also includes a workaround to prevent TextView from crashing on specific Android versions and the possibility to load images from local drawables folder or from the Internet. This library ...
android TextView 支持CSS样式