android的默认字体和你设计的软件有区别。你要实现的设计感需要用到一些特殊的字体。这个经验可以解决你的问题。
工具/原料
- 编译工具:Eclipse
方法/步骤
-
首先展示项目结构
步骤阅读
-
页面类
类名称:MainActivity
package com.example.android_fonts_test;
import android.support.v7.app.ActionBarActivity;import android.content.Context;import android.graphics.Typeface;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private Context mContext; private TextView test_font_1; private TextView test_font_2; private TextView test_font_3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; // 字体 Typeface typeface_1 = Typeface.createFromAsset(mContext.getAssets(),"fonts/Beyond Wonderland.ttf"); test_font_1 = (TextView)findViewById(R.id.test_font_1); test_font_1.setTypeface(typeface_1); Typeface typeface_2 = Typeface.createFromAsset(mContext.getAssets(),"fonts/Channel.ttf"); test_font_2 = (TextView)findViewById(R.id.test_font_2); test_font_2.setTypeface(typeface_2); Typeface typeface_3 = Typeface.createFromAsset(mContext.getAssets(),"fonts/JFHolNte.ttf"); test_font_3 = (TextView)findViewById(R.id.test_font_3); test_font_3.setTypeface(typeface_3); }
@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; }
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
步骤阅读
-
页面xml代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.android_fonts_test.MainActivity" >
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<TextView android:id="@+id/test_font_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textSize="16sp" />
<TextView android:id="@+id/test_font_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textSize="16sp" />
<TextView android:id="@+id/test_font_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textSize="16sp" /> </LinearLayout>
</RelativeLayout>
步骤阅读
-
想要调用的字体文件,是一定要放到项目里面的。
步骤阅读
-
关键性的项目代码上面已经展现出来了。
但是我还是提出来,给大家重点提醒下。
步骤阅读
-
例子生成的页面效果
(我为了让效果更明显,添加了三种字体。)
步骤阅读
END
注意事项
- 本步骤需要一定代码基础
- 程序代码以代码片段的形式展示
本文来自投稿,不代表幸运快三立场,转载请注明出处:http://www.morucat.com/digital/9416.html