Android 材料设计和appcompat不适用于旧API版本

Android 材料设计和appcompat不适用于旧API版本,android,material-design,android-appcompat,Android,Material Design,Android Appcompat,我使用appcompat v7(23.1.1)进行材料设计向后兼容性。在API21设备上,它看起来不错。在较旧的API版本上,我的风格不会影响任何内容。为什么? API 21的结果(目标): 导致API 17(不良): 以下是my style.xml inside values文件夹: <resources> <!-- Base application theme, dependent on API level. This theme is replaced

我使用appcompat v7(23.1.1)进行材料设计向后兼容性。在API21设备上,它看起来不错。在较旧的API版本上,我的风格不会影响任何内容。为什么?

API 21的结果(目标):

导致API 17(不良):

以下是my style.xml inside values文件夹:

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
    <!-- customize the color palette -->
    <item name="android:colorPrimary">@color/teal200</item>
    <item name="android:colorPrimaryDark">@color/teal500</item>
    <item name="android:colorAccent">@color/material_green_A200</item>
    <item name="android:statusBarColor">@color/teal500</item>
    <item name="android:navigationBarColor">@color/teal500</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:colorButtonNormal">@color/button</item>
    <item name="colorControlNormal">@color/deeporange300</item>
    <item name="colorControlActivated">@color/deeporange500</item>
    <item name="colorControlHighlight">@color/material_green_A200</item>        
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are specific to a particular API-level befor 21 can go here. -->
</style>

@颜色/茶色200
@颜色/茶色500
@颜色/材料\绿色\ A200
@颜色/茶色500
@颜色/茶色500
符合事实的
错误的
@颜色/按钮
@颜色/深橙色300
@颜色/深橙色500
@颜色/材料\绿色\ A200

以下是values-v21文件夹中的styles.xml:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- Customize your theme using Material Design here. -->
        <item name="android:buttonStyle">@style/button</item>
        <item name="android:buttonStyleToggle">@style/togglebutton</item>
    </style>
    <style name="button" parent="@android:style/Widget.Material.Button">
        <item name="android:textColor">@color/buttontextcolor</item>
    </style>
    <style name="togglebutton" parent="@android:style/Widget.Material.Button.Toggle">
        <item name="android:textColor">@color/buttontextcolor</item>
    </style>
</resources>

@样式/按钮
@样式/切换按钮
@颜色/按钮ExtColor
@颜色/按钮ExtColor
这是布局文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    tools:context="com.skymedium.theblowdryer.MainActivity"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/menue"
        android:id="@+id/menue" >
        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center_vertical"
        android:id="@+id/row1" >

            <Button
                android:id="@+id/tab"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="@string/jukebox" />

            <RelativeLayout
                android:gravity="center_vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_weight="0.5"
                android:id="@+id/subrow" >
                <ImageView
                    android:id="@+id/tooltip"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="15dp"
                    android:layout_alignParentRight="true"
                    android:scaleType="fitCenter"
                    android:src="@drawable/abc_ab_share_pack_holo_dark" />
            </RelativeLayout>

        </LinearLayout>/
        <LinearLayout
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:id="@+id/row2" >
            <ToggleButton
            android:id="@+id/onoff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ToggleButton"
            android:layout_marginRight="5dp"
            android:layout_weight="0.7" />
            <SeekBar
            android:id="@+id/seekBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="0.3" />
        </LinearLayout>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/swipearea" >
        <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_height="wrap_content" />
        <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        android:layout_gravity="top"
        android:src="@drawable/abc_ab_share_pack_holo_dark" />

    </LinearLayout>



</LinearLayout>

/
我试图设置
@color/button\u color
它在较旧的API上不起作用


提前谢谢

你把它倒过来了。让我解释一下

styles.xml

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
    <!-- customize the color palette -->


    <!--Notice!!! No "android" prefix!-->
    <item name="colorPrimary">@color/teal200</item>
    <item name="colorPrimaryDark">@color/teal500</item>
    <item name="colorAccent">@color/material_green_A200</item>



    <item name="android:statusBarColor">@color/teal500</item>
    <item name="android:navigationBarColor">@color/teal500</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:colorButtonNormal">@color/button</item>
    <item name="colorControlNormal">@color/deeporange300</item>
    <item name="colorControlActivated">@color/deeporange500</item>
    <item name="colorControlHighlight">@color/material_green_A200</item>        
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are specific to a particular API-level befor 21 can go here. -->
</style>

@颜色/茶色200
@颜色/茶色500
@颜色/材料\绿色\ A200
@颜色/茶色500
@颜色/茶色500
符合事实的
错误的
@颜色/按钮
@颜色/深橙色300
@颜色/深橙色500
@颜色/材料\绿色\ A200
样式-v21

<resources>
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- Customize your theme using Material Design here. -->


        <!--Be sure to define your colors here as well! but this time use "android:"-->
        <item name="android:colorPrimary">@color/teal200</item>
        <item name="android:colorPrimaryDark">@color/teal500</item>
        <item name="android:colorAccent">@color/material_green_A200</item>





        <item name="android:buttonStyle">@style/button</item>
        <item name="android:buttonStyleToggle">@style/togglebutton</item>
    </style>
    <style name="button" parent="@android:style/Widget.Material.Button">
        <item name="android:textColor">@color/buttontextcolor</item>
    </style>
    <style name="togglebutton" parent="@android:style/Widget.Material.Button.Toggle">
        <item name="android:textColor">@color/buttontextcolor</item>
    </style>
</resources>

@颜色/茶色200
@颜色/茶色500
@颜色/材料\绿色\ A200
@样式/按钮
@样式/切换按钮
@颜色/按钮ExtColor
@颜色/按钮ExtColor

在styles.xml v-21中,使用android前缀定义颜色。上面的XML应该可以工作

你把它倒过来了。让我解释一下

styles.xml

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
    <!-- customize the color palette -->


    <!--Notice!!! No "android" prefix!-->
    <item name="colorPrimary">@color/teal200</item>
    <item name="colorPrimaryDark">@color/teal500</item>
    <item name="colorAccent">@color/material_green_A200</item>



    <item name="android:statusBarColor">@color/teal500</item>
    <item name="android:navigationBarColor">@color/teal500</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:colorButtonNormal">@color/button</item>
    <item name="colorControlNormal">@color/deeporange300</item>
    <item name="colorControlActivated">@color/deeporange500</item>
    <item name="colorControlHighlight">@color/material_green_A200</item>        
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are specific to a particular API-level befor 21 can go here. -->
</style>

@颜色/茶色200
@颜色/茶色500
@颜色/材料\绿色\ A200
@颜色/茶色500
@颜色/茶色500
符合事实的
错误的
@颜色/按钮
@颜色/深橙色300
@颜色/深橙色500
@颜色/材料\绿色\ A200
样式-v21

<resources>
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- Customize your theme using Material Design here. -->


        <!--Be sure to define your colors here as well! but this time use "android:"-->
        <item name="android:colorPrimary">@color/teal200</item>
        <item name="android:colorPrimaryDark">@color/teal500</item>
        <item name="android:colorAccent">@color/material_green_A200</item>





        <item name="android:buttonStyle">@style/button</item>
        <item name="android:buttonStyleToggle">@style/togglebutton</item>
    </style>
    <style name="button" parent="@android:style/Widget.Material.Button">
        <item name="android:textColor">@color/buttontextcolor</item>
    </style>
    <style name="togglebutton" parent="@android:style/Widget.Material.Button.Toggle">
        <item name="android:textColor">@color/buttontextcolor</item>
    </style>
</resources>

@颜色/茶色200
@颜色/茶色500
@颜色/材料\绿色\ A200
@样式/按钮
@样式/切换按钮
@颜色/按钮ExtColor
@颜色/按钮ExtColor

在styles.xml v-21中,使用android前缀定义颜色。上面的XML应该可以工作

AppCompat会自动在API 21上构建每个版本的
android:
版本-根本不需要styles-v21。@ianhanniballake这很有趣,我必须尝试一下!谢谢你对我代码的改进。不幸的是,样式属性仍然不影响较旧的api版本(例如按钮颜色)。@skymedium-这正是我对Zielony说的:)AppCompat自动在api 21上构建每个版本的
android:
版本-根本不需要样式-v21。@ianhanniballake这很有趣,我必须尝试一下!谢谢你对我代码的改进。不幸的是,样式属性仍然不会影响较旧的api版本(例如按钮颜色)。@skymedium-这正是我对Zielony说的:)