【问题标题】:Using material on pre-Lollipop sdk在 pre-Lollipop sdk 上使用材料
【发布时间】:2014-10-28 16:31:14
【问题描述】:

两天以来,我尝试在一个新应用(sdk 20-)中使用材料设计。

我阅读了一些教程,使用这个 [http://android-developers.blogspot.fr/2014/10/appcompat-v21-material-design-for-pre.html][1] 我有这个错误:

C:\Users\Maxime\Desktop\MaterialApp\app\src\main\res\values\themes.xml 错误:检索项目的父项时出错:找不到与给定名称“android:Theme.AppCompat.Light”匹配的资源。 错误:任务“:app:processDebugResources”执行失败。 com.android.ide.common.internal.LoggedErrorException:无法运行命令: C:\Users\Maxime\AppData\Local\Android\android-studio3\sdk\build-tools\21.0.2\aapt.exe package -f --no-crunch -I C:\Users\Maxime\AppData\Local\ Android\android-studio3\sdk\platforms\android-21\android.jar -M C:\Users\Maxime\Desktop\MaterialApp\app\build\intermediates\manifests\debug\AndroidManifest.xml -S C:\Users\Maxime\桌面\MaterialApp\app\build\intermediates\res\debug -A C:\Users\Maxime\Desktop\MaterialApp\app\build\intermediates\assets\debug -m -J C:\Users\Maxime\Desktop\MaterialApp\app\ build\generated\source\r\debug -F C:\Users\Maxime\Desktop\MaterialApp\app\build\intermediates\libs\app-debug.ap_ --debug-mode --custom-package com.maxime.myapplication - 0 apk --output-text-symbols C:\Users\Maxime\Desktop\MaterialApp\app\build\intermediates\symbols\debug 错误代码: 1 输出: C:\Users\Maxime\Desktop\MaterialApp\app\build\intermediates\res\debug\values\values.xml:2411:错误:检索项目的父项时出错:未找到与给定名称匹配的资源 'android:Theme. AppCompat.Light'。

我添加了我创建的文件来获得这个:

/res/values/themes.xml

<resources>
<style name="Theme.MyTheme" parent="android:Theme.AppCompat.Light">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/yellow</item>

<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/blue</item>

<!-- colorAccent is used as the default value for colorControlActivated,
     which is used to tint widgets -->
<item name="colorAccent">@color/green</item>

<!-- You can also set colorControlNormal, colorControlActivated
     colorControlHighlight, and colorSwitchThumbNormal. -->

</style>

/res/layout/activity_home.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.maxime.materialapp.HomeActivity">

<android.support.v7.widget.Toolbar
    layout_height="match_parent"
    layout_width="match_parent"
    android:id="@+id/toolbar"
    minHeight="?attr/actionBarSize"
    background="?attr/colorPrimary" />

/java/package/HomeActivity.java

public class HomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

最后,setSupportActionBar(toolbar) 无法识别。

还有我的 gradle:`apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.0.2"

    defaultConfig {
        applicationId "com.maxime.myapplication"
        minSdkVersion 19
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "com.android.support:appcompat-v7:21.0.+"
}

【问题讨论】:

    标签: android android-appcompat material-design


    【解决方案1】:

    AppCompat 主题是Theme.AppCompat.Light,而不是android:Theme.AppCompat.Light

    【讨论】:

      【解决方案2】:

      尝试将您的 styles.xml 更改为 &lt;style name="AppTheme" parent="android:Theme.Material"&gt; 并参考本教程 https://developer.android.com/training/material/index.html

      【讨论】:

      • 除非将 minSdkVersion 设置为 21,否则这将不起作用。要支持 pre-Lollipop,您需要使用某种形式的 Theme.AppCompat
      【解决方案3】:

      如果您要使用工具栏的支持版本 (android.support.v7.widget.Toolbar),则需要同时使用 ActionBarActivitysetSupportActionBar 而不是 Activity

      Activity.setActionBar(android.widget.Toolbar toolbar) 是在 API 级别 21 中添加的,因此不能与 minSdkVersion 19 一起使用

      另外,正如 Ian Lake 所说,您必须使用 Theme.AppCompat.Light,而不是 android:Theme.AppCompat.Light

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        • 2015-07-11
        • 2015-12-11
        • 2014-08-27
        相关资源
        最近更新 更多