How do I dynamically change the text color of a theme?
I would like the change the text color of all TextViews 开发者_开发百科once the user chooses a different font color.
I can achieve this by linking all associated TextViews and call setTextColor on them.
But I would like to know if this could also be done through customizing Themes?
This is an old question, but nevertheless, I seem to have an answer.
In its simplest form.
<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>
In your androidmanifest set;
<activity
android:name=".SomeActivity"
android:label="@string/app_name"
android:theme="@style/BaseTheme" />
Then in your 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);
...
I tried without starting a new activity, but it doesn't reset the colors.
精彩评论