TextView uses a Custom font and is highlighted. textview is highlighted.

Source: Internet
Author: User

TextView uses a Custom font and is highlighted. textview is highlighted.

Respect Originality:Http://blog.csdn.net/yuanzeyao/article/details/40478815

Currently, many applications like to use custom fonts. Today I will use custom fonts in TextView and set the highlight. The display effect is as follows:



Define LedTextView. java

/*** Highlighted TextView * com. led. demo. ledTextView * @ author yuanzeyao <br/> * create at October 26, 2014 6:46:05 */public class LedTextView extends TextView {private static final String TAG = "LedTextView"; public LedTextView (Context context) {super (context); init (context);} public LedTextView (Context context, AttributeSet attrs) {super (context, attrs); init (context );} public LedTextView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init (context);} private void init (Context context) {// Add the font resource to the assets folder. AssetManager am = context. getAssets (); Typeface font = Typeface. createFromAsset (am, "fonts/digital-7.ttf"); setTypeface (font );}

Reference LedTextView in layout File

<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:background="@android:color/black"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <com.led.demo.LedTextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#00ff00"        android:text="12:00:00"        android:shadowColor="#00ff00"        android:shadowDx="0"        android:shadowDy="0"        android:textSize="20sp"         /></RelativeLayout>

After the settings are completed, the above results will be displayed.


Android TextView font change

. Custom font
Android Typeface uses TTF font files to set fonts

We can put the ttf font file in the program and set the font using Typeface in the program.
Step 1: Create the fonts directory under the assets Directory and put the ttf font file here.
Step 2: Call in the program:
AssetManager mgr = getAssets (); // get AssetManager
Typeface tf = Typeface. createFromAsset (mgr, "fonts/ttf. ttf"); // obtain Typeface Based on the path
TV. setTypeface (tf); // set the font

2. Use android: textStyle = "bold" in the xml file to set English to bold, but not Chinese to bold,
The method to set Chinese to bold is:
TextView TV = (TextView) findViewById (R. id. TextView01 );
TV. getPaint (). setFakeBoldText (true); // Chinese imitation "bold" -- use TextPaint imitation "bold" to set setFakeBoldText to true.
Hi.baidu.com/...4.html
Hi.baidu.com/...9.html

How to set the font in TextView in android

Changing the Font Type attribute is Typeface.
By default, Android only supports
Sans,
Serif,
Monospace
Three built-in fonts.
If you want to use a Custom font, import the ttf of the font to the resource and then reference it (the process is not simple, please google)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.