为什么我的棒棒糖动作条显示任何颜色(实际上colorPrimary)?我的、棒棒糖、颜色、动作

2023-09-12 03:23:40 作者:泪痣﹌

我写了这个SSCCE来说明问题。模拟器是NexusFive但它定制了使用API​​22。

SSCCE 的

RES /价值/ colors.xml

 < XML版本=1.0编码=UTF-8&GT?;

<资源>
    <颜色名称=primaryColor>#69F0AE< /彩色> &所述;! - 光ferozi  - >
    <颜色名称=primaryColorDark>#00E676< /彩色> <! - 较深Ferozi  - >
    <颜色名称=accentColor>#F44336< /彩色> <! - 红500  - >
< /资源>
 

RES /价值/ styles.xml

 <资源>

    <样式名称=AppBaseTheme父=Theme.AppCompat>< /风格>


    <样式名称=AppTheme父=AppBaseTheme>
        <项目名称=colorPrimary> @色/ primaryColor< /项目>
        <项目名称=colorPrimaryDark> @色/ primaryColorDark< /项目>
        <项目名称=colorAccent> @色/ accentColor< /项目>
    < /风格>

< /资源>
 
你看到的显示器色彩是否真实

RES /值-V21 / styles.xml

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <样式名称=AppTheme父=AppBaseTheme>
        <项目名称=机器人:colorPrimary> @色/ primaryColor< /项目>
        <项目名称=机器人:colorPrimaryDark> @色/ primaryColorDark< /项目>
        <项目名称=机器人:colorAccent> @色/ accentColor< /项目>
    < /风格>
< /资源>
 

activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s

    机器人:layout_width =match_parent
    机器人:layout_height =match_parent

    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin

    工具:上下文=practice_projects.material_design_google_now_like_searchbox_four.MainActivity>

    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/参考hello world/>

< / RelativeLayout的>
 

MainActivity.java

 公共类MainActivity扩展AppCompatActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。将操作栏
        //自动在主/向上按钮操作的点击,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT的id = item.getItemId();
        如果(ID == R.id.action_settings){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }
}
 

解决方案

这是 AppCompatActivity 将无法识别安卓colorPrimary 。如果您只更改这些属性,这是完全没有必要创建一个V21 styles.xml。如果您自定义其他属性V21,那么只需将颜色属性的基本主题和V21 styles.xml删除它们。

I wrote this SSCCE to demonstrate the problem. The emulator is NexusFive but it is customised to use API22.

SSCCE

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <color name="primaryColor">#69F0AE</color> <!-- Light ferozi -->
    <color name="primaryColorDark">#00E676</color> <!-- Darker Ferozi -->
    <color name="accentColor">#F44336</color> <!-- Red 500 -->
</resources>

res/values/styles.xml

<resources>

    <style name="AppBaseTheme" parent="Theme.AppCompat"></style>


    <style name="AppTheme" parent="AppBaseTheme">
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColorDark</item>
        <item name="colorAccent">@color/accentColor</item>
    </style>

</resources>

res/values-v21/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:colorPrimary">@color/primaryColor</item>
        <item name="android:colorPrimaryDark">@color/primaryColorDark</item>
        <item name="android:colorAccent">@color/accentColor</item>
    </style>
</resources>

activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="practice_projects.material_design_google_now_like_searchbox_four.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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);
    }
}

解决方案

An AppCompatActivity will not recognize android:colorPrimary. If you are only changing these properties, it is completely unnecessary to create a v21 styles.xml. If you are customizing other properties for v21, then just move your color properties to the base theme and delete them from v21 styles.xml.