除了文字颜色之外,与文字对象息息相关的文字大小(Size)及字体(font)是整个TextView文字实例的最后一站.
本例通过单击按钮来改变TextVies的字体大小与字体
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white">
<!-- 文字以mytextview為id
引用string.xml的textview_str參數
文字使用color.xml中定義的drawable顏色 -->
<TextView
android:id="@+id/mytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview_str"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:textColor="@drawable/blue" />
<!-- 改變大小按鈕以sizebutton為id
引用sizebutton_str並置中安排於文字下方 -->
<Button
android:id="@+id/sizebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sizebutton_str"
android:layout_below="@+id/mytextview"
android:layout_centerHorizontal="true" />
<!-- 改變字型按鈕以fontbutton為id
引用fontbutton_str並與sizebutton並排 -->
<Button
android:id="@+id/fontbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fontbutton_str"
android:layout_alignTop="@+id/sizebutton"
android:layout_toLeftOf="@+id/sizebutton" />
</RelativeLayout>
MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace Ex03_14
{
[Activity(Label = "Ex03_14", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private TextView mText;
private Button sizeButton;
private Button fontButton;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
mText = (TextView)FindViewById(Resource.Id.mytextview);
sizeButton = (Button)FindViewById(Resource.Id.sizebutton);
fontButton = (Button)FindViewById(Resource.Id.fontbutton);
sizeButton.Click += delegate {
mText.SetTextSize(Android.Util.ComplexUnitType.Px, 20);
};
fontButton.Click += delegate
{
mText.SetTypeface(Android.Graphics.Typeface.DefaultBold, Android.Graphics.TypefaceStyle.Bold);
};
}
}
}