Saturday, September 3, 2011

Apply style on Text

Apply style on Text

To create our own style, create a XML file /res/values/style.xml.

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

<resources>
<style name="mystyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#A0A0A0</item>
<item name="android:typeface">monospace</item>
</style>
<style name="mystyle.20dp">
<item name="android:textSize">20dp</item>
</style>
<style name="mystyle.30dp">
<item name="android:textSize">30dp</item>
</style>
<style name="mystyle.20dp.bold">
<item name="android:textStyle">bold</item>
</style>
</resources>




Then, we can refer it in our layout, main.xml. Android also pre-defined some style for using.

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
style="@style/mystyle"
android:text="mystyle"
/>
<TextView
style="@style/mystyle.20dp"
android:text="mystyle.20dp"
/>
<TextView
style="@style/mystyle.20dp.bold"
android:text="mystyle.20dp.bold"
/>
<TextView
style="@style/mystyle.30dp"
android:text="mystyle.30dp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/TextAppearance"
android:text="Android build-in style: TextAppearance"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
style="@android:style/TextAppearance.Inverse"
android:text="Android build-in style: TextAppearance.Inverse"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Small"
android:text="Android build-in style: TextAppearance.Small"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Medium"
android:text="Android build-in style: TextAppearance.Medium"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Large"
android:text="Android build-in style: TextAppearance.Large"
/>
</LinearLayout>




No comments:

Post a Comment