Overriding standard Android themes

I managed to override any themes with names with "android:" added to them, but Android themes.xml also defines properties that do not seem to be overridden. For example:

<!-- Variation on the Light theme that turns off the title --> <style name="Theme.Codebase" parent="android:style/Theme.Light"> <item name="android:windowNoTitle">true</item> <item name="android:windowContentOverlay">@null</item> <item name="colorBackground">@color/off_white</item> </style> 

colorBackground is defined in Theme.Light xml, but adding this here gives me

 /res/values/styles.xml:10: error: Error: No resource found that matches the given name: attr 'colorBackground'. 

mistake. How to override this style for the application as a whole?

+8
java android
source share
2 answers

You can overwrite the standard attributes the same way you changed properties like windowNoTitle , just remember to add the android: prefix as follows:

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="SEclubTheme" parent="@android:style/Theme"> <item name="android:colorForeground">@color/bright_foreground_dark</item> <item name="android:colorBackground">@color/background_dark</item> </style> </resources> 
+8
source share

Without the attr prefix, your colorBackground will become the attribute you need to define. Consider the following example where theme_dependent_icon is defined in styles.xml :

 <resources xmlns:android="http://schemas.android.com/apk/res/android"> <declare-styleable name="custom_menu"> <attr name="theme_dependent_icon" format="reference"/> </declare-styleable> <style name="MyDarkTheme" parent="android:Theme" > <item name="theme_dependent_icon">@drawable/ic_search_dark</item> </style> <style name="MyLightTheme" parent="android:Theme.Light" > <item name="theme_dependent_icon">@drawable/ic_search_light</item> </style> </resources> 

Then you can use the attribute through ?attr/theme_dependent_icon in your main_activity.xml :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="?attr/theme_dependent_icon" /> </LinearLayout> 

In this example, since I used custom themes names MyDarkTheme and MyLightTheme , they must be selected during the onCreate your main activity before setContentView , i.e.

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.style.MyDarkTheme); // causes ic_search_dark.png to be shown // setTheme(R.style.MyLightTheme); // causes ic_search_light.png to be shown setContentView(R.layout.main_activity); } 

Calling setTheme () is one way to select a theme at runtime. Another way is to define several versions of styles.xml in your resources in values , values-11 , values-14 , corresponding to the default theme, theme for Android 3.0 (API-11) and theme for Android 4.0 (API-14).

+3
source share

All Articles