天天看點

3.14 MonoForAndroid使用者人機界面--控制不同的文字字型

除了文字顔色之外,與文字對象息息相關的文字大小(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);
            };

        }
    }
}