Sei sulla pagina 1di 6

Fonts in XML

Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you
use fonts as resources. You can add the font file in the res/font/ folder to bundle
fonts as resources. These fonts are compiled in your R file and are automatically
available in Android Studio. You can access the font resources with the help of a
new resource type, font. For example, to access a font resource,
use @font/myfont, or R.font.myfont.

To use the Fonts in XML feature on devices running Android 4.1 (API level 16) and
higher, use the Support Library 26. For more information on using the support library,
refer to the Using the support library section.

To add fonts as resources, perform the following steps in the Android Studio:

1. Right-click the res folder and go to New > Android resource directory.
The New Resource Directory window appears.
2. In the Resource type list, select font, and then click OK.
Note: The name of the resource directory must be font.
Figure 1. Adding the font resource directory
3. Add your font files in the font folder.
The folder structure below generates R.font.dancing_script, R.font.lobster,
andR.font.typo_graphica.
Figure 2. Adding the font files in the resource directory
4. Double-click a font file to preview the file's fonts in the editor.

Figure 3. Previewing the font file

Creating a font family


A font family is a set of font files along with its style and weight details. In Android,
you can create a new font family as an XML resource and access it as a single unit,
instead of referencing each style and weight as separate resources. By doing this,
the system can select the correct font based on the text style you are trying to use.

To create a font family, perform the following steps in the Android Studio:

1. Right-click the font folder and go to New > Font resource file. The New Resource
File window appears.
2. Enter the file name, and then click OK. The new font resource XML opens in the
editor.
3. Enclose each font file, style, and weight attribute in the <font> element. The
following XML illustrates adding font-related attributes in the font resource XML:

<?xml version="1.0" encoding="utf-8"?>


<font-family
xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/lobster_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/lobster_italic" />
</font-family>

Adding fonts to a TextView

To set a font for the TextView, do one of the following:

 In the layout XML file, set the fontFamily attribute to the font file you want to access.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lobster"/>

o Mountain View, CA on November 7-8, 2018.

                           

  

                                

                                         
   

   

 Styles and Themes


Styles and themes on Android allow you to separate the details of your app design from the
UI structure and behavior, similar to stylesheets in web design.

A style is a collection of attributes that specify the appearance for a single View. A style can
specify attributes such as font color, font size, background color, and much more.

A theme is a type of style that's applied to an entire app, activity, or view hierarchy, not just
an individual view. When you apply your style as a theme, every view in the app or activity
applies each style attribute that it supports. Themes can also apply styles to non-view
elements, such as the status bar and window background.

Styles and themes are declared in a style resource file in res/values/, usually named
styles.xml.
Figure 1. Two themes applied to the same activity: Theme.AppCompat (left) and
Theme.AppCompat.Light (right)

Create and apply a style

To create a new style or theme, open your project's res/values/styles.xml file. For each
style you want to create, follow these steps:

1. Add a <style> element with a name that uniquely identifies the style.
2. Add an <item> element for each style attribute you want to define.

The name in each item specifies an attribute you would otherwise use as an XML
attribute in your layout. The value in the <item> element is the value for that
attribute.

For example, if you define the following style:

<?xml version="1.0" encoding="utf-8"?>


<resources>
<style name="GreenText" parent="TextAppearance.AppCompat">
<item name="android:textColor">#00FF00</item>
</style>
</resources>
Apply a style as a theme

You can create a theme the same way you create styles. The difference is how you apply it:
instead of applying a style with the style attribute on a view, you apply a theme with the
android:theme attribute on either the <application> tag or an <activity> tag in the
AndroidManifest.xml file.

For example, here's how to apply the Android Support Library's material design "dark" theme
to the whole app:
<manifest ... >
<application android:theme="@style/Theme.AppCompat" ... >
</application>
</manifest>

And here's how to apply the "light" theme to just one activity:

<manifest ... >


<application ... >
<activity android:theme="@style/Theme.AppCompat.Light" ... >
</activity>
</application>
</manifest>

Now every view in the app or activity applies the styles defined in the given theme. If a view
supports only some of the attributes declared in the style, then it applies only those attributes
and ignores the ones it does not support.

<?xml version="1.0" encoding="utf-8"?>

The XML declaration in the document map consists of the following:

 The version number, <?xml version="1.0"?>.

This is mandatory. Although the number might change for future versions of XML,
1.0 is the current version.

 The encoding declaration, <?xml version="1.0" encoding="UTF-8"?>

This is optional. If used, the encoding declaration must appear immediately after the
version information in the XML declaration, and must contain a value representing an
existing character encoding.

The encoding declaration identifies which encoding is used to represent the characters in the
document

Stands for "Unicode Transformation Format." UTF refers to several types of Unicode character
encodings, including UTF-7, UTF-8, UTF-16, and UTF-32.

Potrebbero piacerti anche