求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   模型库  
会员   
 


AI 智能化软件测试方法与实践
5月23-24日 上海+在线



人工智能.机器学习TensorFlow
5月22-23日 北京



图数据库与知识图谱
5月22-23日 北京
 
 
 

Android开发教程
Android 开发环境配置
Android 架构
Android 应用组件
Android Hello World示例
Android 资源组织和访问
Android Activity
Android Service
Android 广播接收器
Android 内容提供者
Android 碎片/片段
Android Intent过滤器
Android UI布局
Android UI控件
Android 事件处理
Android 样式和主题
Android 自定义组件
Android 拖放
Android 通知
Android 基于位置服务
Android 发送电子邮件
Android 发送短信/SMS
Android 拨打电话
发布Android应用
ndroid Alertdialog
Android Animation实例
Android音频捕获
Android音频管理器实例
Android
Android最佳实践
Android Bluetooth实例
Android Camera
Android Clipboard
Android自定义字体
Android数据备份
Android Gestures/手势
Android图片效果
Android图片切换
Android内部存储
Android JetPlayer实例
Android JSON解析器
Android加载Spinner
Android本地化
Android登录实例
Android MediaPlayer
 
 

Android自定义字体
1018 次浏览
69次  

在Android中,可以定义自己的自定义字体,在应用程序中的字符串。只需要从互联网上下载所需的字体,然后将其放置在?assets/fonts?文件夹中。

在字体文件放到 assets/fonts?文件夹后,在Java代码中可以通过?Typeface类访问它。首先,获取在代码文本视图的参考。它的语法如下:?

TextView tx = (TextView)findViewById(R.id.textview1);

需要做的下一件事就是调用Typeface类的createFromAsset()静态方法,从assets的自定义字体。它的语法如下:

Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");

需要做的最后一件事就是这个自定义字体对象设置TextView的字体属性。需要调用setTypeface()方法来做到这一点。它的语法如下:?

tx.setTypeface(custom_font);

除了这些方法,还有在字体类中定义的其它方法,可以使用更有效地处理字体。

例子

这里有一个例子演示如何使用字体的处理CustomFont。它创建一个显示字体文件中指定的自定义字体的基本应用。

为了试验这个例子,可以在实际设备或模拟器运行此程序。

以下是修改后的主活动文件的内容?src/com.yiibai.customfonts/MainActivity.java.

package com.example.customfonts;
    import android.app.Activity;  import android.graphics.Typeface;
    import android.os.Bundle;  import android.view.Menu;
    import android.widget.TextView;
    public class MainActivity extends Activity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tx = (TextView)findViewById(R.id.hello);
        Typeface custom_font = Typeface.createFromAsset(getAssets(),
        "fonts/Erika Type.ttf");
        tx.setTypeface(custom_font);
     }
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
     }
 }  

以下是XML的经修改的内容?res/layout/activity_main.xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical">
       <TextView
        android:id="@+id/hello"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="70dip"
        android:text="@string/hello_world" />
    </LinearLayout>

以下是?res/values/string.xml.的内容

<?xml version="1.0" encoding="utf-8"?>
    <resources>
       <string name="app_name">CustomFonts</string>
       <string name="action_settings">Settings</string>
       <string name="hello_world">Hello</string>
    </resources>

以下是?AndroidManifest.xml?的内容.

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.yiibai.customfonts"
     android:versionCode="1"
     android:versionName="1.0" >
       <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
       <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
           android:name="com.yiibai.customfonts.MainActivity"
           android:label="@string/app_name" >
           <intent-filter>
              <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
        </activity>
      </application>
  </manifest>

让我们试着来运行上面的程序,这里只是修改自定义字体。安装程序在AVD并启动它,如果一切设置和应用程序都没有问题,它会显示以下仿真器窗口:

正如看到的,出现在AVD的文本还没有一个默认的Android字体,而是在字体文件夹中指定的自定义字体。

注意:需要考虑使用自定义字体字体大小,和支持的字符。


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



1018 次浏览
69次
 捐助