2011-05-06 57 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