简体   繁体   中英

What is a theme and how do I know if I have the “correct” one?

In my current project, when I select the Design tab in Android Studio 2.2.2 for a particular layout I get an error that says

Missing styles. Is the correct theme chosen for this layout?

and it goes on to say

Failed to find style 'textEditSuggestionItemLayout' in current theme (42 similar errors not shown)

But, the actual layout renders OK in the Designer and at runtime.

There are lots of other SO posts on this, such as here and here and most of the answers seem to involve clearing the caches and restarting Android Studio, or selecting a different theme from the dropdown. II have tried the first one but it didn't help. I haven't tried the second one yet because I don't really understand what a theme is.

Questions:

  1. The error implies that there's an error in the theme itself. What is that? Is the theme file part of my project, ie, is it one that I should be creating, editing, and that gets built and shipped as part of my APK or is it only used in the developer IDE?
  2. If I select a different theme from the dropdown how do I know what the "correct" one is?
  3. Since my project builds and runs OK as is, can I just ignore these errors? In other words are these errors in my code or just a problem with the development environment?

Edit: Some additional information after responding to comments, below:

The only place the string 'theme' is used in my manifest is

  android:theme="@style/Theme.FullScreen"

... and FullScreen is the theme specified in the dropdown.

I did a search in my project for the string "textEditSuggestionItemLayout" and Android Studio found no occurrences of it.

If you've recently updated AS or any of its components, then try to 'Invalidate caches & restart'.

Else, try choosing AppTheme from the Design tab or change it to AppTheme from the default one. Choose the one that conforms to your parent app theme in your styles.xml .

The cause of the error could be anything from malfunctioning IDE to selecting a theme not mentioned in your styles.xml (most likely).

You could do a project-wide search for textEditSuggestionItemlayout , or the other styles that it claims to miss.

These are either in your own res/values/styles.xml and/or res/values/themes.xml files, or they are provided within the SDK, which is why you see some suggestions to uncheck 'Automatically Pick Best' and pick a version you have installed. In some cases, the latest API version's rendering tools don't work.

As an example, if you have your style set at Theme.Holo , but you are using the AppCompatActivity , you'll get a rendering error that you must use a Theme.AppCompat (or descendant). These themes are changeable from the dropdown of the design editor.

You can see what themes/styles are applied to your Activities within the AndroidManifest.xml .


res/values/styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/PrimaryColor</item>
    <item name="colorPrimaryDark">@color/PrimaryDarkColor</item>
    <!-- Customize your theme here. -->
</style>

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

Note: android:theme= and @style/


If you are still missing themes, then you may be missing some dependency, such as

compile 'com.android.support:design:<your_version_here>' 

If your code runs fine, then sure, ignore it, but I think getting the layout designer working again shouldn't be ignored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM