MonoDroid学习笔记(五)—— 更改TextView字体颜色和背景色及Style样式的定义

本文详细介绍在Android开发中如何使用TextView控件显示文本,包括通过资源文件定义字符串常量并在布局文件中引用,以及如何通过C#代码直接设置文本。此外还介绍了如何设置TextView的字体颜色、背景色以及应用样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在winform或webform中,均有Control类,虽然不在同一个程序集中,但都代表“控件”的意思。在Monodroid中,表示控件的类是Android.Views.View。开发过winform或webform的朋友都知道,要在界面上显示文字,只要使用Label控件即可。在Android中,则要使用TextView。

要设置一个TextView的文本有两种方法,一是使用资源文件来定义一个字符串常数,然后在布局文件中进行引用。二是直接在后台程序中使用C#代码进行赋值。

我们先来试试第一种方法。在Resources/Values/Strings.xml文件中增加一个项,如下所示:

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   <string name="Hello">Hello World, Click Me!</string>  
  4.   <string name="ApplicationName">MonoDroidTest</string>  
  5.   <string name="myText">这是通过Strings.xml设置的文本</string>  
  6. </resources>  
 

然后在布局文件Main.axml中,增加一个TextView,文本引用这个新增加的字符串常量,如下所示:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.                 android:layout_width="fill_parent"  
  4.                 android:layout_height="fill_parent">  
  5.   <TextView android:text="@string/myText"  
  6.             android:layout_x="30px"  
  7.             android:layout_y="0px"  
  8.             android:layout_width="wrap_content"  
  9.             android:layout_height="wrap_content"  
  10.             android:id="@+id/myTextView"/>  
  11. </AbsoluteLayout>  

Activity1.cs中只要把Main.axml设置到ContentView即可:

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using Android.App;  
  3. using Android.Content;  
  4. using Android.Runtime;  
  5. using Android.Views;  
  6. using Android.Widget;  
  7. using Android.OS;  
  8. using MonoDroidTest.Tabs;  
  9. using Android.Util;  
  10. using Java.IO;  
  11. namespace MonoDroidTest  
  12. {  
  13.     [Activity(Label = "MonoDroidTest", MainLauncher = true)]  
  14.     public class Activity1 : Activity  
  15.     {  
  16.         protected override void OnCreate(Bundle bundle)  
  17.         {  
  18.             base.OnCreate(bundle);  
  19.             SetContentView(Resource.Layout.Main);  
  20.         }  
  21.     }  
  22. }  
 

运行之后,就可以看到程序中出现了一行字符串,与我们在Strings.xml中设置的一样:

 

使用代码来设置TextView的文本就更简单了,首先在Activity的OnCreate方法里使用FindViewById方法来通过TextView的id从布局文件中找到这个TextView,然后对其Text进行赋值即可:

[c-sharp]  view plain copy print ?
  1. using Android.Content;  
  2. using Android.Runtime;  
  3. using Android.Views;  
  4. using Android.Widget;  
  5. using Android.OS;  
  6. using MonoDroidTest.Tabs;  
  7. using Android.Util;  
  8. using Java.IO;  
  9. namespace MonoDroidTest  
  10. {  
  11.     [Activity(Label = "MonoDroidTest", MainLauncher = true)]  
  12.     public class Activity1 : Activity  
  13.     {  
  14.         protected override void OnCreate(Bundle bundle)  
  15.         {  
  16.             base.OnCreate(bundle);  
  17.             SetContentView(Resource.Layout.Main);  
  18.             TextView tv = FindViewById<TextView>(Resource.Id.myTextView);  
  19.             tv.Text = "这是通过程序设置的文本";  
  20.         }  
  21.     }  
  22. }  
 

这里提示一下,TextView不支持HTML标签的输出,所以即便写成这样:tv.Text = "<a href=/"http://blog.youkuaiyun.com/ojlovecd/" >区健的博客 </a>"; 实际输出时也不会变成超链接,但若在TextView里加上了android:autoLink="all",或者在程序中使用tv.AutoLinkMask=1,那么文本中若有网址(http://),是可以被转换成超链接的:

 

下面我们试试更改TextView的字体颜色和背景色。在第三篇文章里我们在最后就已经试过把程序的背景色给改成了白色,是通过在Values文件下增加了一个color.xml来设置颜色常量来达到的。但实际设计中最常用的方法是使用程序控制TextView或其它对象的背景颜色。接下来我们在Layout中预先设计好两个TextView对象,并在Activity的OnCreate方法中通过两种方式更改TextView的文字颜色及背景色。

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using Android.App;  
  3. using Android.Content;  
  4. using Android.Runtime;  
  5. using Android.Views;  
  6. using Android.Widget;  
  7. using Android.OS;  
  8. using MonoDroidTest.Tabs;  
  9. using Android.Util;  
  10. using Java.IO;  
  11. namespace MonoDroidTest  
  12. {  
  13.     [Activity(Label = "MonoDroidTest", MainLauncher = true)]  
  14.     public class Activity1 : Activity  
  15.     {  
  16.         protected override void OnCreate(Bundle bundle)  
  17.         {  
  18.             base.OnCreate(bundle);  
  19.             SetContentView(Resource.Layout.Main);  
  20.             SetContentView(Resource.Layout.Main);  
  21.             TextView tv1 = FindViewById<TextView>(Resource.Id.myTextView);  
  22.             tv1.Text = "这是用Drawable设置背景色的文本";  
  23.             tv1.SetBackgroundDrawable(this.Resources.GetDrawable(Resource.Color.white));  
  24.   
  25.             TextView tv2 = FindViewById<TextView>(Resource.Id.myTextView2);  
  26.             tv2.Text = "这是用SetTextColor设置文本颜色的文本";  
  27.             tv2.SetTextColor(Android.Graphics.Color.Green);  
  28.         }  
  29.     }  
  30. }  
 

 

那么定义在Strings.xml中的字符串常量我们要是在程序里要使用的话该怎么获取呢?可以使用Activity中的GetString方法:

tv1.Text = this.GetString(Resource.String.myText)。注意一点的是,如果string.xml中的字符串常数有单引号',双引号"和斜杠/等特殊字符时,记得使用转义字符(/):<string name="myText">这是?通过/'Strings.xml/"设置的文//本</string>

Android可以设置为随着窗口大小调整缩放比例,但即便如此,开发人员还是必须知道手机屏幕的边界,以免造成布局变形问题。获取手机分辨率的方法很简单,关键是DisplayMetrics类的应用。

[c-sharp]  view plain copy print ?
  1. TextView tv2 = FindViewById<TextView>(Resource.Id.myTextView2);  
  2. DisplayMetrics dm = new DisplayMetrics();  
  3. this.WindowManager.DefaultDisplay.GetMetrics(dm);  
  4. tv2.Text = string.Format("手机分辨率为:{0}×{1}", dm.WidthPixels, dm.HeightPixels);  
 

 

老是要一个个指定文字的大小、颜色是不是太麻烦了点?有没有类似CSS那样的方法来指定这些样式呢?事实上是有的,在MonoDroid中,也可以通过样式(Style)的方式,来更改Layout中任何对象的外观。

我们首先在Values文件下增加一个style.xml作为我们的样式文件,然后往里面增加两种样式作为例子:

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   <mce:style name="TextStyle1"><!--  
  4.     <item name="android:textSize">18sp</item>  
  5.     <item name="android:textColor">#ec9237</item>  
  6.     
  7. --></mce:style><style name="TextStyle1" mce_bogus="1">    <item name="android:textSize">18sp</item>  
  8.     <item name="android:textColor">#ec9237</item>  
  9.   </style>  
  10.   <mce:style name="TextStyle2"><!--  
  11.     <item name="android:textSize">14sp</item>  
  12.     <item name="android:textColor">#ff7f7c</item>  
  13.     <item name="android:fromAlpha">0.0</item>  
  14.     <item name="android:toAlpha">0.0</item>  
  15.     
  16. --></mce:style><style name="TextStyle2" mce_bogus="1">    <item name="android:textSize">14sp</item>  
  17.     <item name="android:textColor">#ff7f7c</item>  
  18.     <item name="android:fromAlpha">0.0</item>  
  19.     <item name="android:toAlpha">0.0</item>  
  20.   </style>  
  21. </resources>  
 

然后在布局文件中指定Style属性,使其应用style.xml里事先定义好的样式。如果vs在style下面有波浪提示你未声明“style”特性,不要管它,直接生成。

[xhtml]  view plain copy print ?
  1. <TextView android:text="@string/myText"  
  2.           android:layout_width="wrap_content"  
  3.           android:layout_height="wrap_content"  
  4.           android:id="@+id/myTextView"  
  5.           style="@style/TextStyle1" mce_style="@style/TextStyle1" />  
  6. <TextView android:id="@+id/myTextView2"  
  7.           android:layout_width="wrap_content"  
  8.           android:layout_height="wrap_content"  
  9.           android:layout_y="30px"  
  10.           style="@style/TextStyle2" mce_style="@style/TextStyle2"/>  
 

 

转载http://blog.youkuaiyun.com/ojlovecd/article/details/6300745

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值