Jan 1, 2012

Create style inheriting properties from another style

Refer to the style of last post Create style using Android Resources Tools, we can create a new style(MyStyle.GreenBold) inheriting properties from another existing style (MyStyle).

Modify /res/values/strings.xml, add a new style "MyStyle.GreenBold" inherit properties from "MyStyle", change and add properties of "android:textColor" and "android:textStyle".
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#AAA</item>
<item name="android:textSize">18dip</item>
<item name="android:background">#222</item>
<item name="android:padding">5dip</item>
</style>
<style name="MyStyle.GreenBold">
<item name="android:textColor">#00FF00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>


Modify main.xml adding TextView using new style of "MyStyle.GreenBold".
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
style="@style/MyStyle"
android:layout_width="fill_parent"
android:text="it's text using style" />
<TextView
style="@style/MyStyle.GreenBold"
android:layout_width="fill_parent"
android:text="it's text using style of MyStyle.GreenBold" />
</LinearLayout>


Create style inheriting properties from another style


next:
- Apply style on whole application/activity as theme

No comments:

Post a Comment

Infolinks In Text Ads