Question

I have following style definitions in my res/values/style.xml file:

 <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:textColor">#fff</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="MainActivity" parent="AppTheme">
    <item name="android:textColor">#f0f</item>
    <item name="android:background">@color/black</item> 
</style>

And following code in manifest file:

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:theme="@style/MainActivity"
        android:name="com.example.mygateway.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I expect that textColor #f0f will be applied to MainActivity activity, but it isn't. Neither textColor no other items i tried are applied. Actually the only AppTheme is applied to the activity and the whole application. I'am a beginner in android development. What I do wrong?

Please help. Thanks!

Was it helpful?

Solution

As official docs state:

"If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period. For example, to create a new style that inherits the CodeFont style defined above, but make the color red, you can author the new style like this:"

<style name="AppTheme" parent="AppBaseTheme">   
    <item name="android:textColor">#fff</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="AppTheme.MainActivity">
    <item name="android:textColor">#f0f</item>
    <item name="android:background">@color/black</item> 
</style>

and

<activity android:theme="@style/AppTheme.MainActivity" 
    ...>
       ...
</activity>`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top