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);
            };

        }
    }
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值