Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

android - How to use custom inline attributes in xml layout files for customizing style of views

since when I upgraded the Android Support Library, I'm struggling with setting custom colors for buttons, textviews and other views. Apparently, inline attributes get overridden by values defined in the application theme.

I have defined an application theme in values/styles.xml:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorControlActivated">@color/colorPrimary</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

For instance, in my layout files, I set background and text colors of a button as follows:

<Button
...
android:textColor="@color/colorPrimary"
android:background="@drawable/homebutton"
...
/>

And here's how I define colors in colors.xml:

<resources>
    <color name="colorPrimary">#0066FF</color>
    <color name="colorPrimaryDark">#004AFF</color>
    <color name="colorAccent">#FFFFFF</color>
    <color name="background">#CCCCCC</color>
    <color name="text">#555555</color>
    <color name="green">#009900</color>
    <color name="red">#FF0000</color>
    <color name="orange">#FFA500</color>
    <color name="white">#FFFFFF</color>
    <color name="brown">#7A5230</color>
    <color name="goalkeeper">#FFA500</color>
    <color name="defender">#007c09</color>
    <color name="midfielder">#1d8dc0</color>
    <color name="attacker">#aa0a1c</color>
</resources>

Also, in the Manifest I correctly set the theme:

<application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:theme">

In the preview everything looks good, however when I launch the app in the simulator all inline attributes are ignored and overridden by the values defined by my application theme. Why so? Why are inline attributes ignored? Is there a way to avoid that?

UPDATE: I'm currently following the solution pointed out here https://code.google.com/p/android/issues/detail?id=203136&q=-has%3Ablocked%20reportedby%3DDeveloper%20attachments%3D2%20label%3AComponent-Support-Libraries&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened It may be an issue caused by a bug in Maven Local repository

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Found the cause, hence fixed the issue :-)

Apparently the version 29 of the Maven Local Repository (aka m2repository)is buggy, as pointed out here https://code.google.com/p/android/issues/detail?id=203136&q=-has%3Ablocked%20reportedby%3DDeveloper%20attachments%3D2%20label%3AComponent-Support-Libraries&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened and here https://code.google.com/p/android/issues/detail?id=203546

The solution is simple: just downgrade to version 26, which can be downloaded here http://dl.google.com/android/repository/android_m2repository_r26.zip


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...