我的Material Design ActionBar或StatusBar在Lollipop设备中没有显示颜色,但显示在Pre-Lollipop设备的ActionBar中。为什么?

时间:2015-08-04 09:10:32

标签: android material-design

我跟随this tutorial在Android Material设计中使用(我的ActionBar和StatusBar)中的颜色。我确实按照教程但没有反映颜色,当我在我的API22模拟器中运行应用程序时显示相同的黑暗ActionBar和StatusBar,而颜色显示在Pre-lollipop设备的ActionBar中问题是为什么我该如何解决这个问题?

minimumSdkVersion8targetSdkVersion22。它与5.1.1 API 22一起编译。用于测试Lollipop设备的仿真器是Nexus One,但是可以自定义运行API 22,而用于测试Pre-lollipop设备的仿真器正在运行API 08

MainActivity.java

public class MainActivity extends ActionBarActivity {

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

        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.primaryColorDark)); 
        }

    }

    @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);
    }
}

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"
    tools:context="${relativePackage}.${activityClass}" >

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

</RelativeLayout>

RES /值/ color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primaryColor">#FF5722</color>
    <color name="primaryColorDark">#E64A19</color>
    <color name="accentColor">#9C27B0</color>
</resources>

RES /值/ styles.xml

<resources>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar" ></style>


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

    </style>

</resources>

res/values-v11/styles.xmlres/values-14/styles.xml中存在相同的(如^)代码。

RES /值-V21 / styles.xml

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

    <style name="AppTheme" parent="AppTheme.Base">
        <item name="android:colorPrimary">@color/primaryColor</item>
        <item name="android:colorPrimaryDark">@color/primaryColorDark</item>
        <item name="android:colorAccent">@color/accentColor</item>
    </style>

</resources>

res/values-v22/styles.xml中有相同的(如^)代码。

编辑左边的是运行API 22,右边的是运行API 08。

enter image description here

2 个答案:

答案 0 :(得分:1)

MainActivity.java 中的

变化

public class MainActivity extends ActionBarActivity {

public class MainActivity extends AppCompatActivity {
res / values / styles.xml

中的

用这个替换所有内容

<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">#e23c7f</item>
    <item name="colorPrimaryDark">#dae22b</item>
    <item name="colorAccent">#5863e3</item>
</style>

如果以上都不起作用,请在onCreate()方法中添加以下代码段

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources() 
.getColor(R.color.primaryColorDark)));

最后你会得到这样的东西:

enter image description here

答案 1 :(得分:0)

看来你正在使用api 22;在样式21.xml中将android:colorPrimary更改为colorPrimary,就像在旧设备的styles.xml中一样,以及来自v21.xml的样式中的所有其余颜色值