1. 程式人生 > >android 新增自定義theme和style

android 新增自定義theme和style

在資原始檔vaule目錄中新增檔案theme.xml,內容如下:

<resources>

    <style name="Theme.Test"
        parent="android:Theme">
        <item name="android:windowBackground">@drawable/myselfdrawable</item>
        <item name="android:textColor">#0000ff</item>
          <item name="android:textSize">10sp</item>
          <item name="android:textStyle">bold</item>
    </style>    

</resources>

myselfdrawable 可以是圖片,也可以是檔案,為檔案時檔名為myselfdrawable.xml, 內容如下:

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

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/sheep"
    android:tileMode="repeat" />

之後即可在AndroidManefest.xml中便可引用該theme,

<application
        android:icon="@drawable/ic_launcher"
        android:theme="@style/Theme.Test"
        android:label="@string/app_name" >

....

</application>

style 的定義可在value中新增style.xml檔案,內容為:

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

    <style name="MyTestStyle" parent="@android:style/TextAppearance">
        <item name="android:textColor">#00ff00</item>
        <item name="android:textSize">20sp</item>
    </style>

</resources>

則在view中(如textview中)可以引用,如下:

Style="@style/MyTestStyle"

android 系統的theme和style檔案位於:frameworks/base/core/res/res/values/ 目錄下