2011-05-06 67 views
1

我想在用戶選擇不同的字體顏色後,更改所有TextView的文本顏色。如何動態更改主題的文字顏色?

我可以通過鏈接所有關聯的TextViews並在其上調用setTextColor來實現此目的。

但我想知道這是否也可以通過自定義主題完成?

+0

AFAIK號您提到的方式是要走的路。 – MByD 2011-05-06 07:17:13

+0

這真是令人難過,考慮到電視的數量可能有... – 2011-05-06 07:22:56

+1

使用ArrayList或類似的東西來鏈接所有這些電視機,沒那麼糟糕。 – MByD 2011-05-06 07:24:21

回答

1

這是一個老問題, 但不過, 我似乎有一個答案。

以其最簡單的形式。

<style name="BaseTheme" parent="@android:style/Theme.Black"> 
    <item name="android:textColor">@color/white</item> 
    <item name="android:background">@color/black</item> 
</style> 

<style name="InvertedTheme" parent="BaseTheme"> 
    <item name="android:textColor">@color/black</item> 
    <item name="android:background">@color/white</item> 
</style> 

在你的androidmanifest集合中;

<activity 
    android:name=".SomeActivity" 
    android:label="@string/app_name" 
    android:theme="@style/BaseTheme" /> 

然後在你的SomeActivity.java中;

public class SomeActivity extends Activity { 

    static final String INVERTED_EXTRA = "inverted"; 

    private void invertTheme() { 
    // to make the theme take effect we need to restart the activity 
    Intent inverted = new Intent(this, SomeActivity.class); 
    inverted.putExtra(INVERTED_EXTRA, true); 
    startActivity(inverted); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // must be before the setContentView 
    if (getIntent().getBooleanExtra(INVERTED_EXTRA, false)) 
     setTheme(R.style.InvertedTheme); 
    } 

    setContentView(R.layout.some_layout); 
    ... 

我嘗試不開始新的活動, 但它不重置顏色。

+0

+1爲你的辛勤工作,希望sb解決這個問題:D:D – 2011-11-12 01:59:08