2011-02-08 138 views
4

我的應用程序有一個通用的主要活動。 主要業務讀取arrays.xml它的配置一樣遵循動態更改活動主題

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string-array name="home_buttons_text"> 
    <item>@string/Title_1</item> 
    </string-array> 
    <array name="home_buttons_icon"> 
    <item>@drawable/google</item> 
    </array> 
    <string-array name="home_buttons_url"> 
    <item>http://www.google.com</item> 
    </string-array> 
    <array name="themes"> 
    <item>@style/my_theme</item> 
    </array> 
</resources> 

通過這種方式來添加一個新的圖標,我只是把新的生產線在這裏。

所以在「主題」中我有主題的名字。我如何加載和應用名稱引用的主題?

對於圖標我用

TypedArray mItemsIcons = resources.obtainTypedArray(R.array.home_buttons_icon); 
TypedArray mThemes = resources.obtainTypedArray(R.array.themes); 
. 
. 
. 
mItemsIcons.getDrawable(position); 
. 
. 
. 

但我可如何讓主題不知道。 我測試過

int theme = mThemes.getResourceId(position); 
setTheme(theme); 

但它不起作用。

檢查查看詳情:http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray

回答

4

你可以調用setContentView(...)super.oncreate()之前使用setTheme(..),它應該正常工作

+0

這真的幫了我:http://javarticles.com/2015/04/android-設置主題dynamically.html – 8m47x 2015-08-07 10:31:01